Skip to content

Commit

Permalink
Improved log_format directive configuration: added escaping and multi…
Browse files Browse the repository at this point in the history
…line support
  • Loading branch information
Aleksei Maslov authored and pleshakov committed Feb 21, 2020
1 parent ae85531 commit 24dc092
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
6 changes: 4 additions & 2 deletions internal/configs/config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 28 additions & 4 deletions internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions internal/configs/version1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions internal/configs/version1/nginx-plus.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" '
Expand Down Expand Up @@ -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 '
Expand Down
10 changes: 8 additions & 2 deletions internal/configs/version1/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" '
Expand Down Expand Up @@ -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 '
Expand Down
5 changes: 4 additions & 1 deletion internal/configs/version1/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 24dc092

Please sign in to comment.