github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/agent/syslog.go (about) 1 package agent 2 3 import ( 4 "bytes" 5 6 "github.com/hashicorp/go-syslog" 7 "github.com/hashicorp/logutils" 8 ) 9 10 // levelPriority is used to map a log level to a 11 // syslog priority level 12 var levelPriority = map[string]gsyslog.Priority{ 13 "TRACE": gsyslog.LOG_DEBUG, 14 "DEBUG": gsyslog.LOG_INFO, 15 "INFO": gsyslog.LOG_NOTICE, 16 "WARN": gsyslog.LOG_WARNING, 17 "ERR": gsyslog.LOG_ERR, 18 "CRIT": gsyslog.LOG_CRIT, 19 } 20 21 // SyslogWrapper is used to cleaup log messages before 22 // writing them to a Syslogger. Implements the io.Writer 23 // interface. 24 type SyslogWrapper struct { 25 l gsyslog.Syslogger 26 filt *logutils.LevelFilter 27 } 28 29 // Write is used to implement io.Writer 30 func (s *SyslogWrapper) Write(p []byte) (int, error) { 31 // Skip syslog if the log level doesn't apply 32 if !s.filt.Check(p) { 33 return 0, nil 34 } 35 36 // Extract log level 37 var level string 38 afterLevel := p 39 x := bytes.IndexByte(p, '[') 40 if x >= 0 { 41 y := bytes.IndexByte(p[x:], ']') 42 if y >= 0 { 43 level = string(p[x+1 : x+y]) 44 afterLevel = p[x+y+2:] 45 } 46 } 47 48 // Each log level will be handled by a specific syslog priority 49 priority, ok := levelPriority[level] 50 if !ok { 51 priority = gsyslog.LOG_NOTICE 52 } 53 54 // Attempt the write 55 err := s.l.WriteLevel(priority, afterLevel) 56 return len(p), err 57 }