-
Notifications
You must be signed in to change notification settings - Fork 14
/
syslog_appender.go
71 lines (57 loc) · 1.27 KB
/
syslog_appender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// +build !windows
package logging
import (
"log/syslog"
)
//SysLogAppender is the logging appender for appending to the syslog service
type SysLogAppender struct {
BaseLogAppender
syslogger *syslog.Writer
}
/*
NewSysLogAppender creates a sys log appender
*/
func NewSysLogAppender() *SysLogAppender {
appender := new(SysLogAppender)
appender.level = DEFAULT
return appender
}
/*
Log adds a record to the sys log
*/
func (appender *SysLogAppender) Log(record *LogRecord) error {
if !appender.CheckLevel(record.Level) {
return nil
}
if appender.syslogger == nil {
logWriter, e := syslog.New(syslog.LOG_DEBUG, "")
if e == nil {
appender.syslogger = logWriter
} else {
return e
}
}
if appender.syslogger != nil {
formatted := appender.format(record)
switch record.Level {
case DEBUG:
return appender.syslogger.Debug(formatted)
case INFO:
return appender.syslogger.Info(formatted)
case WARN:
return appender.syslogger.Warning(formatted)
case ERROR:
return appender.syslogger.Err(formatted)
default:
return appender.syslogger.Debug(formatted)
}
}
return nil
}
//Close shuts down the syslog connection
func (appender *SysLogAppender) Close() error {
if appender.syslogger != nil {
return appender.syslogger.Close()
}
return nil
}