-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog.go
136 lines (114 loc) · 2.54 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package log
import (
"fmt"
"io/ioutil"
stdlog "log"
"log/syslog"
)
type syslogOption interface {
applySyslog(*syslogLogger) error
}
type syslogLogger struct {
level Level
flags flags
printLevel Level
tag string
addr string
network string
loggers []*stdlog.Logger
closers []func()
stdHandler bool
stdSorter logSorter
}
// Write satisfies io.Writer interface so that syslogLogger can be used as writer for the standard global logger.
func (s *syslogLogger) Write(p []byte) (n int, err error) {
level := s.stdSorter(p)
logger := s.getLoggerByLevel(level)
if logger == nil {
return
}
err = logger.Output(4, string(p))
return
}
func (s *syslogLogger) PrintLevel() Level {
return s.printLevel
}
func (s *syslogLogger) Close() {
s.level = Disabled
for _, closer := range s.closers {
closer()
}
}
func (s *syslogLogger) Level() Level {
return s.level
}
func (s *syslogLogger) getLoggerByLevel(level Level) *stdlog.Logger {
if !level.IsEnabled(s.level) {
return nil
}
if level > Error {
level = Error
}
return s.loggers[level-s.level]
}
func (s *syslogLogger) Logf(level Level, format string, value ...interface{}) {
logger := s.getLoggerByLevel(level)
if logger == nil {
return
}
_ = logger.Output(3, fmt.Sprintf(format, value...))
}
var syslogDefaultOptions, _ = Options(
commonOptions,
WithUTCTimestamp(false),
WithMicrosecondsTimestamp(false),
)
// NewSyslog creates a new syslog logger with the specified options.
func NewSyslog(opt ...Option) (log Log, err error) {
l := &syslogLogger{}
// apply default options first
if err = syslogDefaultOptions.applySyslog(l); err != nil {
err = newConfigError(err)
return
}
// apply any specified options
for _, o := range opt {
if err = o.applySyslog(l); err != nil {
err = newConfigError(err)
return
}
}
if l.level == Disabled {
return
}
for i := l.level; i <= Error; i++ {
var w *syslog.Writer
w, err = syslog.Dial(l.network, l.addr, toSyslogPriority(i), l.tag)
if err != nil {
err = newConnectionError(err)
return
}
logger := stdlog.New(w, "", int(l.flags))
l.loggers = append(l.loggers, logger)
l.closers = append(l.closers, func() {
// stop using the writer before closing it
logger.SetOutput(ioutil.Discard)
_ = w.Close()
})
}
return Log{logger: l}, nil
}
func toSyslogPriority(l Level) syslog.Priority {
switch l {
case Debug:
return syslog.LOG_DEBUG
case Warning:
return syslog.LOG_WARNING
case Error:
return syslog.LOG_ERR
case Info:
fallthrough
default:
return syslog.LOG_INFO
}
}