Browse code

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

Signed-off-by: Dennis Docter <dennis@d23.nl>

Dennis Docter authored on 2015/06/08 20:37:54
Showing 2 changed files
... ...
@@ -3,12 +3,14 @@
3 3
 package syslog
4 4
 
5 5
 import (
6
+	"errors"
6 7
 	"io"
7 8
 	"log/syslog"
8 9
 	"net"
9 10
 	"net/url"
10 11
 	"os"
11 12
 	"path"
13
+	"strconv"
12 14
 	"strings"
13 15
 
14 16
 	"github.com/Sirupsen/logrus"
... ...
@@ -18,6 +20,29 @@ import (
18 18
 
19 19
 const name = "syslog"
20 20
 
21
+var facilities = map[string]syslog.Priority{
22
+	"kern":     syslog.LOG_KERN,
23
+	"user":     syslog.LOG_USER,
24
+	"mail":     syslog.LOG_MAIL,
25
+	"daemon":   syslog.LOG_DAEMON,
26
+	"auth":     syslog.LOG_AUTH,
27
+	"syslog":   syslog.LOG_SYSLOG,
28
+	"lpr":      syslog.LOG_LPR,
29
+	"news":     syslog.LOG_NEWS,
30
+	"uucp":     syslog.LOG_UUCP,
31
+	"cron":     syslog.LOG_CRON,
32
+	"authpriv": syslog.LOG_AUTHPRIV,
33
+	"ftp":      syslog.LOG_FTP,
34
+	"local0":   syslog.LOG_LOCAL0,
35
+	"local1":   syslog.LOG_LOCAL1,
36
+	"local2":   syslog.LOG_LOCAL2,
37
+	"local3":   syslog.LOG_LOCAL3,
38
+	"local4":   syslog.LOG_LOCAL4,
39
+	"local5":   syslog.LOG_LOCAL5,
40
+	"local6":   syslog.LOG_LOCAL6,
41
+	"local7":   syslog.LOG_LOCAL7,
42
+}
43
+
21 44
 type Syslog struct {
22 45
 	writer *syslog.Writer
23 46
 }
... ...
@@ -39,10 +64,15 @@ func New(ctx logger.Context) (logger.Logger, error) {
39 39
 		return nil, err
40 40
 	}
41 41
 
42
+	facility, err := parseFacility(ctx.Config["syslog-facility"])
43
+	if err != nil {
44
+		return nil, err
45
+	}
46
+
42 47
 	log, err := syslog.Dial(
43 48
 		proto,
44 49
 		address,
45
-		syslog.LOG_DAEMON,
50
+		facility,
46 51
 		path.Base(os.Args[0])+"/"+tag,
47 52
 	)
48 53
 	if err != nil {
... ...
@@ -102,3 +132,20 @@ func parseAddress(address string) (string, string, error) {
102 102
 
103 103
 	return "", "", nil
104 104
 }
105
+
106
+func parseFacility(facility string) (syslog.Priority, error) {
107
+	if facility == "" {
108
+		return syslog.LOG_DAEMON, nil
109
+	}
110
+
111
+	if syslogFacility, valid := facilities[facility]; valid {
112
+		return syslogFacility, nil
113
+	}
114
+
115
+	fInt, err := strconv.Atoi(facility)
116
+	if err == nil && 0 <= fInt && fInt <= 23 {
117
+		return syslog.Priority(fInt << 3), nil
118
+	}
119
+
120
+	return syslog.Priority(0), errors.New("invalid syslog facility")
121
+}
... ...
@@ -891,6 +891,7 @@ The following logging options are supported for this logging driver:
891 891
 
892 892
     --log-opt syslog-address=[tcp|udp]://host:port
893 893
     --log-opt syslog-address=unix://path
894
+    --log-opt syslog-facility=daemon
894 895
     --log-opt syslog-tag="mailer"
895 896
 
896 897
 `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`
901 901
 
902 902
     $ docker run --log-driver=syslog --log-opt syslog-address=tcp://192.168.0.42:123
903 903
 
904
-`syslog-tag` specifies tag for syslog messages from container.
904
+The `syslog-facility` option configures the syslog facility. By default, the system uses the
905
+`daemon` value. To override this behavior, you can provide an integer of 0 to 23 or any of
906
+the following named facilities:
907
+
908
+* `kern`
909
+* `user`
910
+* `mail`
911
+* `daemon`
912
+* `auth`
913
+* `syslog`
914
+* `lpr`
915
+* `news`
916
+* `uucp`
917
+* `cron`
918
+* `authpriv`
919
+* `ftp`
920
+* `local0`
921
+* `local1`
922
+* `local2`
923
+* `local3`
924
+* `local4`
925
+* `local5`
926
+* `local6`
927
+* `local7`
928
+
929
+The `syslog-tag` specifies a tag that identifies the container's syslog messages. By default,
930
+the system uses the first 12 characters of the container id. To override this behavior, specify
931
+a `syslog-tag` option
905 932
 
906 933
 #### Logging driver: journald
907 934