github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/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 6 7 import ( 8 "fmt" 9 10 "github.com/Sirupsen/logrus" 11 "github.com/coreos/go-systemd/journal" 12 "github.com/docker/docker/daemon/logger" 13 ) 14 15 const name = "journald" 16 17 type journald struct { 18 Jmap map[string]string 19 } 20 21 func init() { 22 if err := logger.RegisterLogDriver(name, New); err != nil { 23 logrus.Fatal(err) 24 } 25 } 26 27 // New creates a journald logger using the configuration passed in on 28 // the context. 29 func New(ctx logger.Context) (logger.Logger, error) { 30 if !journal.Enabled() { 31 return nil, fmt.Errorf("journald is not enabled on this host") 32 } 33 // Strip a leading slash so that people can search for 34 // CONTAINER_NAME=foo rather than CONTAINER_NAME=/foo. 35 name := ctx.ContainerName 36 if name[0] == '/' { 37 name = name[1:] 38 } 39 jmap := map[string]string{ 40 "CONTAINER_ID": ctx.ContainerID[:12], 41 "CONTAINER_ID_FULL": ctx.ContainerID, 42 "CONTAINER_NAME": name} 43 return &journald{Jmap: jmap}, nil 44 } 45 46 func (s *journald) Log(msg *logger.Message) error { 47 if msg.Source == "stderr" { 48 return journal.Send(string(msg.Line), journal.PriErr, s.Jmap) 49 } 50 return journal.Send(string(msg.Line), journal.PriInfo, s.Jmap) 51 } 52 53 func (s *journald) Close() error { 54 return nil 55 } 56 57 func (s *journald) Name() string { 58 return name 59 }