Skip to content

Commit

Permalink
Add support to define a tag for syslog messages
Browse files Browse the repository at this point in the history
  • Loading branch information
koushki committed Aug 18, 2019
1 parent d765faa commit 94218a8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/ingress/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ type Configuration struct {
SyslogHost string `json:"syslog-host"`
// SyslogPort port
SyslogPort int `json:"syslog-port"`
// SyslogTag changes the tag of messages, default is nginx
SyslogTag string `json:"syslog-tag"`

// NoTLSRedirectLocations is a comma-separated list of locations
// that should not get redirected to TLS
Expand Down
9 changes: 7 additions & 2 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,23 @@ http {
default 1;
}

{{ $syslogOptions := "" }}
{{ if not (empty $cfg.SyslogTag) }}
{{ $syslogOptions = printf ",tag=%s" $cfg.SyslogTag }}
{{ end }}

{{ if $cfg.DisableAccessLog }}
access_log off;
{{ else }}
{{ if $cfg.EnableSyslog }}
access_log syslog:server={{ $cfg.SyslogHost }}:{{ $cfg.SyslogPort }} upstreaminfo if=$loggable;
access_log syslog:server={{ $cfg.SyslogHost }}:{{ $cfg.SyslogPort }}{{ $syslogOptions }} upstreaminfo if=$loggable;
{{ else }}
access_log {{ $cfg.AccessLogPath }} upstreaminfo {{ $cfg.AccessLogParams }} if=$loggable;
{{ end }}
{{ end }}

{{ if $cfg.EnableSyslog }}
error_log syslog:server={{ $cfg.SyslogHost }}:{{ $cfg.SyslogPort }} {{ $cfg.ErrorLogLevel }};
error_log syslog:server={{ $cfg.SyslogHost }}:{{ $cfg.SyslogPort }}{{ $syslogOptions }} {{ $cfg.ErrorLogLevel }};
{{ else }}
error_log {{ $cfg.ErrorLogPath }} {{ $cfg.ErrorLogLevel }};
{{ end }}
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/annotations/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,45 @@ var _ = framework.IngressNginxDescribe("Annotations - Log", func() {
return Expect(server).Should(ContainSubstring(`rewrite_log on;`))
})
})

It("enable syslog", func() {
host := "log.foo.com"
annotations := map[string]string{}
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, "http-svc", 80, &annotations)
f.EnsureIngress(ing)

f.SetNginxConfigMapData(map[string]string{
"enable-syslog": "true",
"syslog-host": "localhost",
})

f.WaitForNginxServer(host,
func(server string) bool {
accessLog := `access_log syslog:server=localhost:514 upstreaminfo if=$loggable;`
errorLog := `error_log syslog:server=localhost:514 notice;`
return Expect(server).Should(ContainSubstring(accessLog)) &&
Expect(server).Should(ContainSubstring(errorLog))
})
})

It("enable syslog with tag", func() {
host := "log.foo.com"
annotations := map[string]string{}
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, "http-svc", 80, &annotations)
f.EnsureIngress(ing)

f.SetNginxConfigMapData(map[string]string{
"enable-syslog": "true",
"syslog-host": "localhost",
"syslog-tag": "mytag",
})

f.WaitForNginxServer(host,
func(server string) bool {
accessLog := `access_log syslog:server=localhost:514,tag=mytag upstreaminfo if=$loggable;`
errorLog := `error_log syslog:server=localhost:514,tag=mytag notice;`
return Expect(server).Should(ContainSubstring(accessLog)) &&
Expect(server).Should(ContainSubstring(errorLog))
})
})
})

0 comments on commit 94218a8

Please sign in to comment.