Skip to content

Improvement for log_format directive configuration #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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