Преглед изворни кода

Add --log-opt to specify facility for syslog driver

Signed-off-by: Dennis Docter <dennis@d23.nl>
Dennis Docter пре 10 година
родитељ
комит
609e7b0a55
2 измењених фајлова са 77 додато и 2 уклоњено
  1. 48 1
      daemon/logger/syslog/syslog.go
  2. 29 1
      docs/reference/run.md

+ 48 - 1
daemon/logger/syslog/syslog.go

@@ -3,12 +3,14 @@
 package syslog
 package syslog
 
 
 import (
 import (
+	"errors"
 	"io"
 	"io"
 	"log/syslog"
 	"log/syslog"
 	"net"
 	"net"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
 	"path"
 	"path"
+	"strconv"
 	"strings"
 	"strings"
 
 
 	"github.com/Sirupsen/logrus"
 	"github.com/Sirupsen/logrus"
@@ -18,6 +20,29 @@ import (
 
 
 const name = "syslog"
 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 {
 type Syslog struct {
 	writer *syslog.Writer
 	writer *syslog.Writer
 }
 }
@@ -39,10 +64,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
+	facility, err := parseFacility(ctx.Config["syslog-facility"])
+	if err != nil {
+		return nil, err
+	}
+
 	log, err := syslog.Dial(
 	log, err := syslog.Dial(
 		proto,
 		proto,
 		address,
 		address,
-		syslog.LOG_DAEMON,
+		facility,
 		path.Base(os.Args[0])+"/"+tag,
 		path.Base(os.Args[0])+"/"+tag,
 	)
 	)
 	if err != nil {
 	if err != nil {
@@ -102,3 +132,20 @@ func parseAddress(address string) (string, string, error) {
 
 
 	return "", "", nil
 	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")
+}

+ 29 - 1
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=[tcp|udp]://host:port
     --log-opt syslog-address=unix://path
     --log-opt syslog-address=unix://path
+    --log-opt syslog-facility=daemon
     --log-opt syslog-tag="mailer"
     --log-opt syslog-tag="mailer"
 
 
 `syslog-address` specifies the remote syslog server address where the driver connects to.
 `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
     $ 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
 #### Logging driver: journald