github.com/ld86/docker@v1.7.1-rc3/daemon/logger/syslog/syslog.go (about) 1 // +build linux 2 3 package syslog 4 5 import ( 6 "io" 7 "log/syslog" 8 "net" 9 "net/url" 10 "os" 11 "path" 12 "strings" 13 14 "github.com/Sirupsen/logrus" 15 "github.com/docker/docker/daemon/logger" 16 "github.com/docker/docker/pkg/urlutil" 17 ) 18 19 const name = "syslog" 20 21 type Syslog struct { 22 writer *syslog.Writer 23 } 24 25 func init() { 26 if err := logger.RegisterLogDriver(name, New); err != nil { 27 logrus.Fatal(err) 28 } 29 } 30 31 func New(ctx logger.Context) (logger.Logger, error) { 32 tag := ctx.ContainerID[:12] 33 34 proto, address, err := parseAddress(ctx.Config["syslog-address"]) 35 if err != nil { 36 return nil, err 37 } 38 39 log, err := syslog.Dial( 40 proto, 41 address, 42 syslog.LOG_DAEMON, 43 path.Base(os.Args[0])+"/"+tag, 44 ) 45 if err != nil { 46 return nil, err 47 } 48 49 return &Syslog{ 50 writer: log, 51 }, nil 52 } 53 54 func (s *Syslog) Log(msg *logger.Message) error { 55 if msg.Source == "stderr" { 56 return s.writer.Err(string(msg.Line)) 57 } 58 return s.writer.Info(string(msg.Line)) 59 } 60 61 func (s *Syslog) Close() error { 62 return s.writer.Close() 63 } 64 65 func (s *Syslog) Name() string { 66 return name 67 } 68 69 func (s *Syslog) GetReader() (io.Reader, error) { 70 return nil, logger.ReadLogsNotSupported 71 } 72 73 func parseAddress(address string) (string, string, error) { 74 if urlutil.IsTransportURL(address) { 75 url, err := url.Parse(address) 76 if err != nil { 77 return "", "", err 78 } 79 80 // unix socket validation 81 if url.Scheme == "unix" { 82 if _, err := os.Stat(url.Path); err != nil { 83 return "", "", err 84 } 85 return url.Scheme, url.Path, nil 86 } 87 88 // here we process tcp|udp 89 host := url.Host 90 if _, _, err := net.SplitHostPort(host); err != nil { 91 if !strings.Contains(err.Error(), "missing port in address") { 92 return "", "", err 93 } 94 host = host + ":514" 95 } 96 97 return url.Scheme, host, nil 98 } 99 100 return "", "", nil 101 }