diff --git a/internal/configs/config_params.go b/internal/configs/config_params.go index 16917ffb6e..b02b5fecbc 100644 --- a/internal/configs/config_params.go +++ b/internal/configs/config_params.go @@ -19,9 +19,11 @@ type ConfigParams struct { MainServerNamesHashBucketSize string MainServerNamesHashMaxSize string MainAccessLogOff bool - MainLogFormat string + MainLogFormat []string + MainLogFormatEscaping string MainErrorLogLevel string - MainStreamLogFormat string + MainStreamLogFormat []string + MainStreamLogFormatEscaping string ProxyBuffering bool ProxyBuffers string ProxyBufferSize string diff --git a/internal/configs/configmaps.go b/internal/configs/configmaps.go index b58171af75..c21e79bfc6 100644 --- a/internal/configs/configmaps.go +++ b/internal/configs/configmaps.go @@ -207,12 +207,34 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool) *ConfigParams { } } - if logFormat, exists := cfgm.Data["log-format"]; exists { - cfgParams.MainLogFormat = logFormat + if logFormat, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "log-format", cfgm, "\n"); exists { + if err != nil { + glog.Error(err) + } else { + cfgParams.MainLogFormat = logFormat + } + } + + if logFormatEscaping, exists := cfgm.Data["log-format-escaping"]; exists { + logFormatEscaping = strings.TrimSpace(logFormatEscaping) + if logFormatEscaping != "" { + cfgParams.MainLogFormatEscaping = logFormatEscaping + } } - if streamLogFormat, exists := cfgm.Data["stream-log-format"]; exists { - cfgParams.MainStreamLogFormat = streamLogFormat + if streamLogFormat, exists, err := GetMapKeyAsStringSlice(cfgm.Data, "stream-log-format", cfgm, "\n"); exists { + if err != nil { + glog.Error(err) + } else { + cfgParams.MainStreamLogFormat = streamLogFormat + } + } + + if streamLogFormatEscaping, exists := cfgm.Data["stream-log-format-escaping"]; exists { + streamLogFormatEscaping = strings.TrimSpace(streamLogFormatEscaping) + if streamLogFormatEscaping != "" { + cfgParams.MainStreamLogFormatEscaping = streamLogFormatEscaping + } } if proxyBuffering, exists, err := GetMapKeyAsBool(cfgm.Data, "proxy-buffering", cfgm); exists { @@ -442,8 +464,10 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config ServerNamesHashMaxSize: config.MainServerNamesHashMaxSize, AccessLogOff: config.MainAccessLogOff, LogFormat: config.MainLogFormat, + LogFormatEscaping: config.MainLogFormatEscaping, ErrorLogLevel: config.MainErrorLogLevel, StreamLogFormat: config.MainStreamLogFormat, + StreamLogFormatEscaping: config.MainStreamLogFormatEscaping, SSLProtocols: config.MainServerSSLProtocols, SSLCiphers: config.MainServerSSLCiphers, SSLDHParam: config.MainServerSSLDHParam, diff --git a/internal/configs/version1/config.go b/internal/configs/version1/config.go index 48ef3cdb6e..7c5f21b320 100644 --- a/internal/configs/version1/config.go +++ b/internal/configs/version1/config.go @@ -127,9 +127,11 @@ type MainConfig struct { ServerNamesHashBucketSize string ServerNamesHashMaxSize string AccessLogOff bool - LogFormat string + LogFormat []string + LogFormatEscaping string ErrorLogLevel string - StreamLogFormat string + StreamLogFormat []string + StreamLogFormatEscaping string HealthStatus bool HealthStatusURI string NginxStatus bool diff --git a/internal/configs/version1/nginx-plus.tmpl b/internal/configs/version1/nginx-plus.tmpl index a89916cdc7..c4058d3d41 100644 --- a/internal/configs/version1/nginx-plus.tmpl +++ b/internal/configs/version1/nginx-plus.tmpl @@ -35,7 +35,10 @@ http { {{- end}} {{if .LogFormat -}} - log_format main '{{.LogFormat}}'; + log_format main {{if .LogFormatEscaping}}escape={{ .LogFormatEscaping }} {{end}} + {{range $i, $value := .LogFormat -}} + {{with $value}}'{{if $i}} {{end}}{{$value}}' + {{end}}{{end}}; {{- else -}} log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' @@ -170,7 +173,10 @@ http { stream { {{if .StreamLogFormat -}} - log_format stream-main '{{.StreamLogFormat}}'; + log_format stream-main {{if .StreamLogFormatEscaping}}escape={{ .StreamLogFormatEscaping }} {{end}} + {{range $i, $value := .StreamLogFormat -}} + {{with $value}}'{{if $i}} {{end}}{{$value}}' + {{end}}{{end}}; {{- else -}} log_format stream-main '$remote_addr [$time_local] ' '$protocol $status $bytes_sent $bytes_received ' diff --git a/internal/configs/version1/nginx.tmpl b/internal/configs/version1/nginx.tmpl index b51e69573b..96e67c2b68 100644 --- a/internal/configs/version1/nginx.tmpl +++ b/internal/configs/version1/nginx.tmpl @@ -34,7 +34,10 @@ http { {{- end}} {{if .LogFormat -}} - log_format main '{{.LogFormat}}'; + log_format main {{if .LogFormatEscaping}}escape={{ .LogFormatEscaping }} {{end}} + {{range $i, $value := .LogFormat -}} + {{with $value}}'{{if $i}} {{end}}{{$value}}' + {{end}}{{end}}; {{- else -}} log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' @@ -161,7 +164,10 @@ http { stream { {{if .StreamLogFormat -}} - log_format stream-main '{{.StreamLogFormat}}'; + log_format stream-main {{if .StreamLogFormatEscaping}}escape={{ .StreamLogFormatEscaping }} {{end}} + {{range $i, $value := .StreamLogFormat -}} + {{with $value}}'{{if $i}} {{end}}{{$value}}' + {{end}}{{end}}; {{- else -}} log_format stream-main '$remote_addr [$time_local] ' '$protocol $status $bytes_sent $bytes_received ' diff --git a/internal/configs/version1/templates_test.go b/internal/configs/version1/templates_test.go index 8a863dcc7c..a761789352 100644 --- a/internal/configs/version1/templates_test.go +++ b/internal/configs/version1/templates_test.go @@ -98,8 +98,11 @@ var mainCfg = MainConfig{ WorkerShutdownTimeout: "1m", WorkerConnections: "1024", WorkerRlimitNofile: "65536", + LogFormat: []string{"$remote_addr", "$remote_user"}, + LogFormatEscaping: "default", StreamSnippets: []string{"# comment"}, - StreamLogFormat: "$remote_addr", + StreamLogFormat: []string{"$remote_addr", "$remote_user"}, + StreamLogFormatEscaping: "none", ResolverAddresses: []string{"example.com", "127.0.0.1"}, ResolverIPV6: false, ResolverValid: "10s",