From 609e7b0a55d4082fce40eabae3a06ca57c188ba5 Mon Sep 17 00:00:00 2001 From: Dennis Docter Date: Mon, 8 Jun 2015 11:37:54 +0000 Subject: [PATCH] Add --log-opt to specify facility for syslog driver Signed-off-by: Dennis Docter --- daemon/logger/syslog/syslog.go | 49 +++++++++++++++++++++++++++++++++- docs/reference/run.md | 30 ++++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/daemon/logger/syslog/syslog.go b/daemon/logger/syslog/syslog.go index b986364310..0b39b16e2d 100644 --- a/daemon/logger/syslog/syslog.go +++ b/daemon/logger/syslog/syslog.go @@ -3,12 +3,14 @@ package syslog import ( + "errors" "io" "log/syslog" "net" "net/url" "os" "path" + "strconv" "strings" "github.com/Sirupsen/logrus" @@ -18,6 +20,29 @@ import ( const name = "syslog" +var facilities = map[string]syslog.Priority{ + "kern": syslog.LOG_KERN, + "user": syslog.LOG_USER, + "mail": syslog.LOG_MAIL, + "daemon": syslog.LOG_DAEMON, + "auth": syslog.LOG_AUTH, + "syslog": syslog.LOG_SYSLOG, + "lpr": syslog.LOG_LPR, + "news": syslog.LOG_NEWS, + "uucp": syslog.LOG_UUCP, + "cron": syslog.LOG_CRON, + "authpriv": syslog.LOG_AUTHPRIV, + "ftp": syslog.LOG_FTP, + "local0": syslog.LOG_LOCAL0, + "local1": syslog.LOG_LOCAL1, + "local2": syslog.LOG_LOCAL2, + "local3": syslog.LOG_LOCAL3, + "local4": syslog.LOG_LOCAL4, + "local5": syslog.LOG_LOCAL5, + "local6": syslog.LOG_LOCAL6, + "local7": syslog.LOG_LOCAL7, +} + type Syslog struct { writer *syslog.Writer } @@ -39,10 +64,15 @@ func New(ctx logger.Context) (logger.Logger, error) { return nil, err } + facility, err := parseFacility(ctx.Config["syslog-facility"]) + if err != nil { + return nil, err + } + log, err := syslog.Dial( proto, address, - syslog.LOG_DAEMON, + facility, path.Base(os.Args[0])+"/"+tag, ) if err != nil { @@ -102,3 +132,20 @@ func parseAddress(address string) (string, string, error) { return "", "", nil } + +func parseFacility(facility string) (syslog.Priority, error) { + if facility == "" { + return syslog.LOG_DAEMON, nil + } + + if syslogFacility, valid := facilities[facility]; valid { + return syslogFacility, nil + } + + fInt, err := strconv.Atoi(facility) + if err == nil && 0 <= fInt && fInt <= 23 { + return syslog.Priority(fInt << 3), nil + } + + return syslog.Priority(0), errors.New("invalid syslog facility") +} diff --git a/docs/reference/run.md b/docs/reference/run.md index 6b0b568dfd..556ec23e16 100644 --- a/docs/reference/run.md +++ b/docs/reference/run.md @@ -891,6 +891,7 @@ The following logging options are supported for this logging driver: --log-opt syslog-address=[tcp|udp]://host:port --log-opt syslog-address=unix://path + --log-opt syslog-facility=daemon --log-opt syslog-tag="mailer" `syslog-address` specifies the remote syslog server address where the driver connects to. @@ -901,7 +902,34 @@ remote server at `192.168.0.42` on port `123` $ docker run --log-driver=syslog --log-opt syslog-address=tcp://192.168.0.42:123 -`syslog-tag` specifies tag for syslog messages from container. +The `syslog-facility` option configures the syslog facility. By default, the system uses the +`daemon` value. To override this behavior, you can provide an integer of 0 to 23 or any of +the following named facilities: + +* `kern` +* `user` +* `mail` +* `daemon` +* `auth` +* `syslog` +* `lpr` +* `news` +* `uucp` +* `cron` +* `authpriv` +* `ftp` +* `local0` +* `local1` +* `local2` +* `local3` +* `local4` +* `local5` +* `local6` +* `local7` + +The `syslog-tag` specifies a tag that identifies the container's syslog messages. By default, +the system uses the first 12 characters of the container id. To override this behavior, specify +a `syslog-tag` option #### Logging driver: journald