forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
189 lines (157 loc) · 4.08 KB
/
log.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
package logp
import (
"fmt"
"log"
"os"
"runtime/debug"
"time"
)
type Priority int
const (
// From /usr/include/sys/syslog.h.
// These are the same on Linux, BSD, and OS X.
LOG_EMERG Priority = iota
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)
type Logger struct {
toSyslog bool
toStderr bool
toFile bool
level Priority
selectors map[string]struct{}
debugAllSelectors bool
logger *log.Logger
syslog [LOG_DEBUG + 1]*log.Logger
rotator *FileRotator
}
// pre-init logger to debug mode + stderr before init
const stderrLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC | log.Lshortfile
var _log = Logger{}
// TODO: remove toSyslog and toStderr from the init function
func LogInit(level Priority, prefix string, toSyslog bool, toStderr bool, debugSelectors []string) {
_log.toSyslog = toSyslog
_log.toStderr = toStderr
_log.level = level
_log.selectors, _log.debugAllSelectors = parseSelectors(debugSelectors)
if _log.toSyslog {
SetToSyslog(true, prefix)
}
if _log.toStderr {
SetToStderr(true, prefix)
}
}
func parseSelectors(selectors []string) (map[string]struct{}, bool) {
all := false
set := map[string]struct{}{}
for _, selector := range selectors {
set[selector] = struct{}{}
if selector == "*" {
all = true
}
}
return set, all
}
func debugMessage(calldepth int, selector, format string, v ...interface{}) {
if _log.level >= LOG_DEBUG && IsDebug(selector) {
send(calldepth+1, LOG_DEBUG, "DBG ", format, v...)
}
}
func send(calldepth int, level Priority, prefix string, format string, v ...interface{}) {
if _log.toSyslog {
_log.syslog[level].Output(calldepth, fmt.Sprintf(format, v...))
}
if _log.toStderr {
_log.logger.Output(calldepth, fmt.Sprintf(prefix+format, v...))
}
if _log.toFile {
// Creates a timestamp for the file log message and formats it
prefix = time.Now().Format(time.RFC3339) + " " + prefix
_log.rotator.WriteLine([]byte(fmt.Sprintf(prefix+format, v...)))
}
}
func Debug(selector string, format string, v ...interface{}) {
debugMessage(3, selector, format, v...)
}
func MakeDebug(selector string) func(string, ...interface{}) {
return func(msg string, v ...interface{}) {
debugMessage(3, selector, msg, v...)
}
}
func IsDebug(selector string) bool {
return _log.debugAllSelectors || HasSelector(selector)
}
func HasSelector(selector string) bool {
_, selected := _log.selectors[selector]
return selected
}
func msg(level Priority, prefix string, format string, v ...interface{}) {
if _log.level >= level {
send(4, level, prefix, format, v...)
}
}
func Info(format string, v ...interface{}) {
msg(LOG_INFO, "INFO ", format, v...)
}
func Warn(format string, v ...interface{}) {
msg(LOG_WARNING, "WARN ", format, v...)
}
func Err(format string, v ...interface{}) {
msg(LOG_ERR, "ERR ", format, v...)
}
func Critical(format string, v ...interface{}) {
msg(LOG_CRIT, "CRIT ", format, v...)
}
// WTF prints the message at CRIT level and panics immediately with the same
// message
func WTF(format string, v ...interface{}) {
msg(LOG_CRIT, "CRIT ", format, v)
panic(fmt.Sprintf(format, v...))
}
func Recover(msg string) {
if r := recover(); r != nil {
Err("%s. Recovering, but please report this: %s.", msg, r)
Err("Stacktrace: %s", debug.Stack())
}
}
func SetToStderr(toStderr bool, prefix string) {
_log.toStderr = toStderr
if _log.toStderr {
// Add timestamp
_log.logger = log.New(os.Stderr, prefix, stderrLogFlags)
}
}
func SetToSyslog(toSyslog bool, prefix string) {
_log.toSyslog = toSyslog
if _log.toSyslog {
for prio := LOG_EMERG; prio <= LOG_DEBUG; prio++ {
_log.syslog[prio] = openSyslog(prio, prefix)
if _log.syslog[prio] == nil {
// syslog not available
_log.toSyslog = false
break
}
}
}
}
func SetToFile(toFile bool, rotator *FileRotator) error {
if toFile {
err := rotator.CreateDirectory()
if err != nil {
return err
}
err = rotator.CheckIfConfigSane()
if err != nil {
return err
}
// Only assign rotator on no errors
_log.rotator = rotator
}
_log.toFile = toFile
return nil
}