-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_syslog.go
97 lines (86 loc) · 2.14 KB
/
handler_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// +build !windows
package logging
import (
"log/syslog"
)
// A handler class which sends formatted logging records to a syslog server.
type SyslogHandler struct {
*BaseHandler
network string
raddr string
priority syslog.Priority
tag string
writer *syslog.Writer
}
// Initialize a syslog handler.
// The arguements are the same as New() in package log/syslog.
func NewSyslogHandler(
priority syslog.Priority,
tag string) (*SyslogHandler, error) {
writer, err := syslog.New(priority, tag)
if err != nil {
return nil, err
}
object := &SyslogHandler{
BaseHandler: NewBaseHandler("", LevelNotset),
network: "",
raddr: "",
priority: priority,
tag: tag,
writer: writer,
}
return object, nil
}
// Initialize a syslog handler with connection to a specified syslog server.
// The arguements are the same as Dial() in package log/syslog.
func NewSyslogHandlerToAddr(
network, raddr string,
priority syslog.Priority,
tag string) (*SyslogHandler, error) {
writer, err := syslog.Dial(network, raddr, priority, tag)
if err != nil {
return nil, err
}
object := &SyslogHandler{
BaseHandler: NewBaseHandler("", LevelNotset),
network: network,
raddr: raddr,
priority: priority,
tag: tag,
writer: writer,
}
return object, nil
}
// Emit a record.
// The record is formatted, and then sent to the syslog server
// in specified log level.
func (self *SyslogHandler) Emit(record *LogRecord) error {
message := self.BaseHandler.Format(record)
var err error
switch record.Level {
case LevelFatal:
err = self.writer.Crit(message)
case LevelError:
err = self.writer.Err(message)
case LevelWarn:
err = self.writer.Warning(message)
case LevelInfo:
err = self.writer.Info(message)
case LevelDebug:
err = self.writer.Debug(message)
default:
_, err = self.writer.Write([]byte(message))
}
return err
}
func (self *SyslogHandler) Handle(record *LogRecord) int {
return self.BaseHandler.Handle2(self, record)
}
func (self *SyslogHandler) Flush() error {
// Nothing to do
return nil
}
func (self *SyslogHandler) Close() {
// ignore the error return code
self.writer.Close()
}