-
Notifications
You must be signed in to change notification settings - Fork 1
/
syslog.go
273 lines (209 loc) · 5.64 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package logy
import (
"io"
"os"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
)
type SyslogHandler struct {
writer io.Writer
enabled atomic.Value
level atomic.Value
format atomic.Value
endpoint atomic.Value
appName atomic.Value
hostname atomic.Value
facility atomic.Value
syslogType atomic.Value
protocol atomic.Value
blockOnReconnect atomic.Value
mu sync.RWMutex
underTest atomic.Value
}
func newSysLogHandler(underTest bool) *SyslogHandler {
handler := &SyslogHandler{}
handler.initializeHandler(underTest)
return handler
}
func (h *SyslogHandler) initializeHandler(underTest bool) {
h.SetEnabled(false)
h.SetLevel(LevelInfo)
h.setWriter(newSyslogWriter(string(ProtocolTCP), DefaultSyslogEndpoint, false, true))
h.SetApplicationName(os.Args[0])
h.SetHostname("")
h.SetFormat(DefaultSyslogFormat)
h.setEndpoint(DefaultSyslogEndpoint)
h.SetFacility(FacilityUserLevel)
h.setLogType(RFC5424)
h.setProtocol(ProtocolTCP)
h.setBlockOnReconnect(false)
h.underTest.Store(underTest)
}
func (h *SyslogHandler) Handle(record Record) error {
facility := h.Facility()
priority := int(facility-1)*8 + int(record.Level.syslogLevel())
buf := newBuffer()
defer buf.Free()
encoder := getTextEncoder(buf)
syslogType := h.syslogType.Load().(SysLogType)
if syslogType == RFC3164 {
h.writeRFC3164Header(buf, priority, record)
} else if syslogType == RFC5424 {
h.writeRFC5424Header(buf, priority, record)
}
format := h.format.Load().(string)
formatText(encoder, format, record, false, true)
putTextEncoder(encoder)
_, err := h.writer.Write(*buf)
return err
}
func (h *SyslogHandler) SetLevel(level Level) {
h.level.Store(level)
}
func (h *SyslogHandler) Level() Level {
return h.level.Load().(Level)
}
func (h *SyslogHandler) SetEnabled(enabled bool) {
h.enabled.Store(enabled)
}
func (h *SyslogHandler) IsEnabled() bool {
return h.enabled.Load().(bool)
}
func (h *SyslogHandler) IsLoggable(record Record) bool {
if !h.IsEnabled() {
return false
}
return record.Level <= h.Level()
}
func (h *SyslogHandler) setWriter(writer io.Writer) {
h.writer = writer
}
func (h *SyslogHandler) Writer() io.Writer {
return h.writer
}
func (h *SyslogHandler) SetFormat(format string) {
h.format.Store(format)
}
func (h *SyslogHandler) Format() string {
return h.format.Load().(string)
}
func (h *SyslogHandler) setEndpoint(endpoint string) {
h.endpoint.Store(endpoint)
}
func (h *SyslogHandler) Endpoint() string {
return h.endpoint.Load().(string)
}
func (h *SyslogHandler) SetApplicationName(name string) {
h.appName.Store(name)
}
func (h *SyslogHandler) ApplicationName() string {
return h.appName.Load().(string)
}
func (h *SyslogHandler) SetHostname(hostname string) {
h.hostname.Store(hostname)
}
func (h *SyslogHandler) Hostname() string {
return h.hostname.Load().(string)
}
func (h *SyslogHandler) SetFacility(facility Facility) {
h.facility.Store(facility)
}
func (h *SyslogHandler) Facility() Facility {
return h.facility.Load().(Facility)
}
func (h *SyslogHandler) setLogType(logType SysLogType) {
h.syslogType.Store(logType)
}
func (h *SyslogHandler) LogType() SysLogType {
return h.syslogType.Load().(SysLogType)
}
func (h *SyslogHandler) setProtocol(protocol Protocol) {
h.protocol.Store(protocol)
}
func (h *SyslogHandler) Protocol() Protocol {
return h.protocol.Load().(Protocol)
}
func (h *SyslogHandler) setBlockOnReconnect(blockOnReconnect bool) {
h.blockOnReconnect.Store(blockOnReconnect)
}
func (h *SyslogHandler) IsBlockOnReconnect() bool {
return h.blockOnReconnect.Load().(bool)
}
func (h *SyslogHandler) OnConfigure(config Config) error {
h.SetEnabled(config.Syslog.Enabled)
h.SetLevel(config.Syslog.Level)
h.SetFormat(config.Syslog.Format)
h.SetApplicationName(config.Syslog.AppName)
h.SetHostname(config.Syslog.Hostname)
h.setProtocol(config.Syslog.Protocol)
h.setLogType(config.Syslog.LogType)
h.SetFacility(config.Syslog.Facility)
h.setBlockOnReconnect(config.Syslog.BlockOnReconnect)
h.setEndpoint(config.Syslog.Endpoint)
network := h.Protocol()
address := h.Endpoint()
underTest := h.underTest.Load().(bool)
sysWriter := h.writer.(*syslogWriter)
defer sysWriter.mu.Unlock()
sysWriter.mu.Lock()
if !underTest {
sysWriter.network = string(network)
sysWriter.address = address
sysWriter.retry = !h.IsBlockOnReconnect()
sysWriter.setDiscarded(false)
return sysWriter.connect()
} else {
sysWriter.setDiscarded(true)
}
return nil
}
func (h *SyslogHandler) writeRFC5424Header(buf *buffer, priority int, record Record) {
hostname := h.Hostname()
appName := h.ApplicationName()
buf.WriteByte('<')
buf.WriteInt(int64(priority))
buf.WriteString(">1 ")
buf.WriteTimeLayout(record.Time, time.RFC3339)
buf.WriteByte(' ')
if hostname == "" {
buf.WriteByte('-')
} else {
buf.WriteString(hostname)
}
buf.WriteByte(' ')
if appName == "" {
buf.WriteByte('-')
} else {
buf.WriteString(appName)
}
buf.WriteByte(' ')
buf.WriteInt(int64(os.Getpid()))
buf.WriteByte(' ')
appendLoggerAsText(buf, record.LoggerName, false, true)
// structured data
buf.WriteString(" - ")
*buf = utf8.AppendRune(*buf, 0xFEFF)
}
func (h *SyslogHandler) writeRFC3164Header(buf *buffer, priority int, record Record) {
hostname := h.Hostname()
appName := h.ApplicationName()
buf.WriteByte('<')
buf.WriteInt(int64(priority))
buf.WriteByte('>')
buf.WriteTimeLayout(record.Time, time.Stamp)
buf.WriteByte(' ')
if hostname == "" {
buf.WriteString("UNKNOWN_HOSTNAME")
} else {
buf.WriteString(hostname)
}
buf.WriteByte(' ')
if appName == "" {
buf.WriteString(appName)
}
buf.WriteByte('[')
buf.WriteInt(int64(os.Getpid()))
buf.WriteString("]: ")
}