github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/daemon/logger/journald/journald.go (about) 1 // +build linux 2 3 // Package journald provides the log driver for forwarding server logs 4 // to endpoints that receive the systemd format. 5 package journald // import "github.com/docker/docker/daemon/logger/journald" 6 7 import ( 8 "fmt" 9 "sync" 10 "unicode" 11 12 "github.com/coreos/go-systemd/journal" 13 "github.com/docker/docker/daemon/logger" 14 "github.com/docker/docker/daemon/logger/loggerutils" 15 "github.com/sirupsen/logrus" 16 ) 17 18 const name = "journald" 19 20 type journald struct { 21 mu sync.Mutex 22 vars map[string]string // additional variables and values to send to the journal along with the log message 23 readers map[*logger.LogWatcher]struct{} 24 closed bool 25 } 26 27 func init() { 28 if err := logger.RegisterLogDriver(name, New); err != nil { 29 logrus.Fatal(err) 30 } 31 if err := logger.RegisterLogOptValidator(name, validateLogOpt); err != nil { 32 logrus.Fatal(err) 33 } 34 } 35 36 // sanitizeKeyMode returns the sanitized string so that it could be used in journald. 37 // In journald log, there are special requirements for fields. 38 // Fields must be composed of uppercase letters, numbers, and underscores, but must 39 // not start with an underscore. 40 func sanitizeKeyMod(s string) string { 41 n := "" 42 for _, v := range s { 43 if 'a' <= v && v <= 'z' { 44 v = unicode.ToUpper(v) 45 } else if ('Z' < v || v < 'A') && ('9' < v || v < '0') { 46 v = '_' 47 } 48 // If (n == "" && v == '_'), then we will skip as this is the beginning with '_' 49 if !(n == "" && v == '_') { 50 n += string(v) 51 } 52 } 53 return n 54 } 55 56 // New creates a journald logger using the configuration passed in on 57 // the context. 58 func New(info logger.Info) (logger.Logger, error) { 59 if !journal.Enabled() { 60 return nil, fmt.Errorf("journald is not enabled on this host") 61 } 62 63 // parse log tag 64 tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate) 65 if err != nil { 66 return nil, err 67 } 68 69 vars := map[string]string{ 70 "CONTAINER_ID": info.ContainerID[:12], 71 "CONTAINER_ID_FULL": info.ContainerID, 72 "CONTAINER_NAME": info.Name(), 73 "CONTAINER_TAG": tag, 74 "IMAGE_NAME": info.ImageName(), 75 "SYSLOG_IDENTIFIER": tag, 76 } 77 extraAttrs, err := info.ExtraAttributes(sanitizeKeyMod) 78 if err != nil { 79 return nil, err 80 } 81 for k, v := range extraAttrs { 82 vars[k] = v 83 } 84 return &journald{vars: vars, readers: make(map[*logger.LogWatcher]struct{})}, nil 85 } 86 87 // We don't actually accept any options, but we have to supply a callback for 88 // the factory to pass the (probably empty) configuration map to. 89 func validateLogOpt(cfg map[string]string) error { 90 for key := range cfg { 91 switch key { 92 case "labels": 93 case "env": 94 case "env-regex": 95 case "tag": 96 default: 97 return fmt.Errorf("unknown log opt '%s' for journald log driver", key) 98 } 99 } 100 return nil 101 } 102 103 func (s *journald) Log(msg *logger.Message) error { 104 vars := map[string]string{} 105 for k, v := range s.vars { 106 vars[k] = v 107 } 108 if msg.PLogMetaData != nil && !msg.PLogMetaData.Last { 109 vars["CONTAINER_PARTIAL_MESSAGE"] = "true" 110 } 111 112 line := string(msg.Line) 113 source := msg.Source 114 logger.PutMessage(msg) 115 116 if source == "stderr" { 117 return journal.Send(line, journal.PriErr, vars) 118 } 119 return journal.Send(line, journal.PriInfo, vars) 120 } 121 122 func (s *journald) Name() string { 123 return name 124 }