-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdaslog.go
304 lines (261 loc) · 8.31 KB
/
daslog.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Package daslog is a simple logger package.
//
// Explanation of the name: DAmn Simple LOGger.
//
// Simple example:
//
// func main() {
// o := daslog.Options{
// Destination: os.Stdout,
// Prefix: "{{.O}} [{{.Q}}]: ",
// LogLevel: daslog.UrgencyLevelNotice,
// }
//
// l, err := daslog.New(o)
// if err != nil {
// fmt.Print(err)
// return
// }
//
// l.Log(daslog.UrgencyLevelNotice, "test notice message")
//
// l.Info("test info message")
//
// l.Errorf("%s %s %s", "test", "error", "message")
// }
//
// Output:
// 2016-01-04 21:16:41 [notice]: test notice message
// 2016-01-04 21:16:41 [info]: test info message
// 2016-01-04 21:16:42 [error]: test error message
//
package daslog
import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"
"text/template"
"time"
)
// UrgencyLevel - declare the level of informativity of log message.
type UrgencyLevel int
// LogHandler - log handler type.
type LogHandler func(urgencyLevel UrgencyLevel, message string)
// predefined UrgencyLevel levels
const (
UrgencyLevelNone UrgencyLevel = iota
UrgencyLevelNotice
UrgencyLevelInfo
UrgencyLevelError
UrgencyLevelCritical
)
// Options - struct, which is used to configure a Daslog.
type Options struct {
// LogLevel allows to choose log level.
// Each level includes messages from levels above, except UrgencyLevelNone.
// UrgencyLevelNone - no messages // 0
// UrgencyLevelNotice - notice // 1
// UrgencyLevelInfo - info // 2
// UrgencyLevelError - error // 3
// UrgencyLevelCritical - critical // 4
//
// Default: 'UrgencyLevelNone'.
LogLevel UrgencyLevel
// Prefix provides the ability to add prefix text to all log messages.
// In addition, you can use commands to display the date and time (these
// format commands are based on the linux 'GNU date' format syntax).
//
// 'GNU date' compatible format commands:
//
// {{.F}} - full date (2016-01-04)
// {{.T}} - time (16:52:36)
// {{.r}} - 12-hour clock time (11:11:04 PM)
//
// {{.Y}} - year (2016)
// {{.y}} - last two digits of year (00..99)
// {{.m}} - month (01..12)
// {{.b}} - locale's abbreviated month name (Jan)
// {{.B}} - locale's full month name (January)
// {{.d}} - day of month (01)
// {{.a}} - locale's abbreviated weekday name (Sun)
// {{.A}} - locale's full weekday name (Sunday)
// {{.H}} - 24-hour clock hour (00..23)
// {{.I}} - 12-hour clock hour (01..12)
// {{.M}} - minute (00..59)
// {{.S}} - second (00..60)
// {{.p}} - locale's equivalent of either AM or PM (AM)
//
// Incompatible format commands:
//
// {{.O}} - same as {{.F}} + {{.T}} (2016-01-04 16:52:36)
// {{.Q}} - message urgency level (info, critical, etc)
Prefix string
// Destination allows to choose the destination for log messages.
//
// Default: 'os.Stdout'.
Destination io.Writer
// Destinations allows to choose several destinations for delivery of log messages.
//
// If 'Destinations' is selected - 'Destination' option will be ignored.
Destinations []io.Writer
// LogHandler takes a log messages to bypass the internal
// mechanism of the message processing.
//
// If LogHandler is selected - all log settings will be ignored.
Handler LogHandler
}
type prefixTemplateData struct {
F, T, Or, Y, Oy, Om string
Ob, B, Od, Oa, A, H string
I, M, S, Op, O, Q string
}
// Daslog - main struct.
type Daslog struct {
options *Options
templateInPrefix bool
timeTemplate string
urgencyInTemplate bool
urgencyList []string
}
// New - return a new copy of Daslog.
func New(options Options) (*Daslog, error) {
l := &Daslog{
options: &options,
}
if l.options.Handler == nil {
l.options.Handler = l.logHandler
}
if l.options.Destination == nil && len(l.options.Destinations) == 0 {
l.options.Destination = os.Stdout
}
if l.options.Prefix != "" {
if t, _ := regexp.MatchString(`{{\s*\.\w\s*}}`, l.options.Prefix); t {
// repleace all unexported letters and exceptions
replaceList := []string{"r", "y", "m", "b", "d", "a", "p", "Q"}
newPrefix := l.options.Prefix
for _, r := range replaceList {
a := regexp.MustCompile(`{{\s*\.` + r + `\s*}}`)
if r == "Q" {
newPrefix = a.ReplaceAllString(newPrefix, "<{Q}>")
l.urgencyInTemplate = true
continue
}
newPrefix = a.ReplaceAllString(newPrefix, "{{.O"+r+"}}")
}
l.options.Prefix = newPrefix
// prepare template
tmpl, err := template.New("prefix").Parse(l.options.Prefix)
if err != nil {
e := regexp.MustCompile("template: prefix:1")
return l, fmt.Errorf("%s\n", e.ReplaceAllString(err.Error(), "daslog: prefix error"))
}
l.templateInPrefix = true
// check unknown format variables and errors
var buf bytes.Buffer
if err := tmpl.Execute(&buf, &prefixTemplateData{}); err != nil {
if e, _ := regexp.MatchString("is not a field of", err.Error()); e {
e := regexp.MustCompile(`<\..*>`).FindAllString(err.Error(), -1)
ev := regexp.MustCompile("(<|>)").ReplaceAllString(e[0], "")
return l, fmt.Errorf("daslog: unknown format variable in prefix: {{%s}}\n", ev)
}
return l, err
}
// call template
buf.Reset()
data := prefixTemplateData{
F: "2006-01-02",
T: "15:04:05",
Or: "3:04:05 PM",
Y: "2006",
Oy: "06",
Om: "01",
Ob: "Jan",
B: "January",
Od: "02",
Oa: "Mon",
A: "Monday",
H: "15",
I: "03",
M: "04",
S: "05",
Op: "PM",
O: "2006-01-02 15:04:05",
}
tmpl.Execute(&buf, &data)
l.timeTemplate = buf.String()
l.urgencyList = []string{
"none",
"notice",
"info",
"error",
"critical",
}
}
}
return l, nil
}
// logHandler - default message handler.
func (l *Daslog) logHandler(urgencyLevel UrgencyLevel, message string) {
if l.options.LogLevel == UrgencyLevelNone {
return
}
if urgencyLevel >= l.options.LogLevel {
prefix := l.options.Prefix
if l.templateInPrefix {
prefix = time.Now().Local().Format(l.timeTemplate)
if l.urgencyInTemplate {
prefix = strings.Replace(prefix, "<{Q}>", l.urgencyList[urgencyLevel], -1)
}
}
if len(l.options.Destinations) != 0 {
for _, destination := range l.options.Destinations {
fmt.Fprintf(destination, "%s%s", prefix, message)
}
return
}
fmt.Fprintf(l.options.Destination, "%s%s", prefix, message)
}
}
// Log - print message to log.
func (l *Daslog) Log(urgencyLevel UrgencyLevel, message string) {
l.options.Handler(urgencyLevel, message+"\n")
}
// Logf - print message to log in printf style :)
func (l *Daslog) Logf(urgencyLevel UrgencyLevel, message string, a ...interface{}) {
l.options.Handler(urgencyLevel, fmt.Sprintf(message, a...))
}
// Notice - print "notice" level message to log.
func (l *Daslog) Notice(message string) {
l.options.Handler(UrgencyLevelNotice, message+"\n")
}
// Noticef - same as a Notice, but in printf style.
func (l *Daslog) Noticef(message string, a ...interface{}) {
l.options.Handler(UrgencyLevelNotice, fmt.Sprintf(message, a...))
}
// Info - print "info" level message to log.
func (l *Daslog) Info(message string) {
l.options.Handler(UrgencyLevelInfo, message+"\n")
}
// Infof - same as a Info, but in printf style.
func (l *Daslog) Infof(message string, a ...interface{}) {
l.options.Handler(UrgencyLevelInfo, fmt.Sprintf(message, a...))
}
// Error - print "error" level message to log.
func (l *Daslog) Error(message string) {
l.options.Handler(UrgencyLevelError, message+"\n")
}
// Errorf - same as a Error, but in printf style.
func (l *Daslog) Errorf(message string, a ...interface{}) {
l.options.Handler(UrgencyLevelError, fmt.Sprintf(message, a...))
}
// Critical - print "critical" level message to log.
func (l *Daslog) Critical(message string) {
l.options.Handler(UrgencyLevelCritical, message+"\n")
}
// Criticalf - same as a Critical, but in printf style.
func (l *Daslog) Criticalf(message string, a ...interface{}) {
l.options.Handler(UrgencyLevelCritical, fmt.Sprintf(message, a...))
}