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