/
log.go
177 lines (148 loc) · 4.12 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
/* Package log contains logging helpers.
EXPERIMENTAL! This may change prior to the final release of this version!
This package provides syslog-style logging messages. See `log/syslog` in Go's
documentation. A notable difference, though, is that this also provides
formatter variants for all of the log levels.
This uses the Context.Log* functions beneath the hood, so any logger
configuration for that will also hold true for this.
*/
package log
import (
"runtime"
"github.com/Masterminds/cookoo"
)
// LogLevel describes the log levels.
type LogLevel uint8
// Log levels correspond to UNIX/syslog logging levels.
const (
LogEmerg LogLevel = iota
LogAlert
LogCrit
LogErr
LogWarning
LogNotice
LogInfo
LogDebug
)
// Labels for log levels.
const (
LabelEmerg = "[emergency] "
LabelAlert = "[alert] "
LabelCrit = "[critical] "
LabelErr = "[error] "
LabelWarning = "[warning] "
LabelNotice = "[notice] "
LabelInfo = "[info] "
LabelDebug = "[debug] "
)
var Label = [8]string{
LabelEmerg,
LabelAlert,
LabelCrit,
LabelErr,
LabelWarning,
LabelNotice,
LabelInfo,
LabelDebug,
}
var Level LogLevel = LogDebug
// Debugging returns true if the level is set to allow debugging.
//
// Whether or not the log message is sent to the underlying logger is determined
// based on the Level. However, using checks like this can prevent doing
// costly debug computations just for the sake of logging.
//
// if Debugging() {
// // Do something expensive.
// costlyOperation()
//
// Debug(c, "msg")
// }
//
// Otherwise, this will write the message to the lower-level logger, which can
// then decide (presumably based on Level) what to do with the message.
func Debugging() bool {
return Level >= LogDebug
}
// Emergf logs an emergency.
func Emergf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogEmerg, msg, args...)
}
// Alertf logs an alert.
func Alertf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogAlert, msg, args...)
}
// Critf logs a critical message.
func Critf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogCrit, msg, args...)
}
// Errf logs an error message.
func Errf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogErr, msg, args...)
}
// Warnf logs a warning.
func Warnf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogWarning, msg, args...)
}
// Noticef logs a notice.
func Noticef(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogNotice, msg, args...)
}
// Infof logs an informational message.
func Infof(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogInfo, msg, args...)
}
// Debugf logs a debug message.
func Debugf(c cookoo.Context, msg string, args ...interface{}) {
sendf(c, LogDebug, msg, args...)
}
// Emergf logs an emergency.
func Emerg(c cookoo.Context, args ...interface{}) {
send(c, LogEmerg, args...)
}
// Alertf logs an alert.
func Alert(c cookoo.Context, args ...interface{}) {
send(c, LogAlert, args...)
}
// Critf logs a critical message.
func Crit(c cookoo.Context, args ...interface{}) {
send(c, LogCrit, args...)
}
// Errf logs an error message.
func Err(c cookoo.Context, args ...interface{}) {
send(c, LogErr, args...)
}
// Warnf logs a warning.
func Warn(c cookoo.Context, args ...interface{}) {
send(c, LogWarning, args...)
}
// Noticef logs a notice.
func Notice(c cookoo.Context, args ...interface{}) {
send(c, LogNotice, args...)
}
// Infof logs an informational message.
func Info(c cookoo.Context, args ...interface{}) {
send(c, LogInfo, args...)
}
// Debugf logs a debug message.
func Debug(c cookoo.Context, args ...interface{}) {
send(c, LogDebug, args...)
}
// Stack dumps a stack trace to the log. It uses the LogDebug level.
//
// This limits the size of the returned stack to 4096 bytes.
func Stack(c cookoo.Context, msg string) {
buff := make([]byte, 4096)
runtime.Stack(buff, false)
send(c, LogDebug, msg, string(buff))
}
func send(c cookoo.Context, l LogLevel, args ...interface{}) {
if Level >= l {
c.Log(Label[l], args...)
}
}
func sendf(c cookoo.Context, l LogLevel, msg string, args ...interface{}) {
if Level >= l {
c.Logf(Label[l], msg, args...)
}
}