-
Notifications
You must be signed in to change notification settings - Fork 27
/
syslog_writer.go
65 lines (49 loc) · 1020 Bytes
/
syslog_writer.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
package log4go
import (
"errors"
"log/syslog"
)
type ShortRecord Record
func (r *ShortRecord) String() string {
return "<" + r.code + "> " + r.info
}
type SyslogWriter struct {
network string
addr string
tag string
writer *syslog.Writer
}
func NewSyslogWriter() *SyslogWriter {
return &SyslogWriter{}
}
func (w *SyslogWriter) SetNetwork(network string) {
w.network = network
}
func (w *SyslogWriter) SetAddr(addr string) {
w.addr = addr
}
func (w *SyslogWriter) SetTag(tag string) {
w.tag = tag
}
func (w *SyslogWriter) Init() (err error) {
w.writer, err = syslog.Dial(w.network, w.addr, syslog.LOG_SYSLOG, w.tag)
return
}
func (w *SyslogWriter) Write(r *Record) (err error) {
s := ((*ShortRecord)(r)).String()
switch r.level {
case DEBUG:
err = w.writer.Debug(s)
case INFO:
err = w.writer.Info(s)
case WARNING:
err = w.writer.Warning(s)
case ERROR:
err = w.writer.Err(s)
case FATAL:
err = w.writer.Crit(s)
default:
err = errors.New("Invalid level")
}
return
}