-
Notifications
You must be signed in to change notification settings - Fork 1
/
syslog.go
65 lines (57 loc) · 1.31 KB
/
syslog.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
// +build !windows,!plan9
package log
import "log/syslog"
// SyslogHandler sends the logging output to syslog.
type SyslogHandler struct {
*BaseHandler
w *syslog.Writer
}
func NewSyslogHandler(tag string) (*SyslogHandler, error) {
// Priority in New constructor is not important here because we
// do not use w.Write() directly.
w, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, tag)
if err != nil {
return nil, err
}
return &SyslogHandler{
BaseHandler: NewBaseHandler(),
w: w,
}, nil
}
func NewSyslogHandlerDial(network, raddr, tag string) (*SyslogHandler, error) {
// Priority in New constructor is not important here because we
// do not use w.Write() directly.
w, err := syslog.Dial(network, raddr, syslog.LOG_INFO|syslog.LOG_USER, tag)
if err != nil {
return nil, err
}
return &SyslogHandler{
BaseHandler: NewBaseHandler(),
w: w,
}, nil
}
func (b *SyslogHandler) Handle(rec *Record) {
message := b.BaseHandler.FilterAndFormat(rec)
if message == "" {
return
}
var fn func(string) error
switch rec.Level {
case CRITICAL:
fn = b.w.Crit
case ERROR:
fn = b.w.Err
case WARNING:
fn = b.w.Warning
case NOTICE:
fn = b.w.Notice
case INFO:
fn = b.w.Info
case DEBUG:
fn = b.w.Debug
}
fn(message)
}
func (b *SyslogHandler) Close() error {
return b.w.Close()
}