forked from go-playground/log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
143 lines (102 loc) · 4.23 KB
/
Copy pathdoc.go
File metadata and controls
143 lines (102 loc) · 4.23 KB
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
/*
Package log is a simple, highly configurable, structured logging that is a near drop in replacement for the std library log.
Usage
package main
import (
"github.com/go-playground/log"
"github.com/go-playground/log/handlers/console"
)
func main() {
cLog := console.New()
log.RegisterHandler(cLog, log.AllLevels...)
// Trace
defer log.Trace("trace").End()
log.Debug("debug")
log.Info("info")
log.Notice("notice")
log.Warn("warn")
log.Error("error")
// log.Panic("panic") // this will panic
log.Alert("alert")
// log.Fatal("fatal") // this will call os.Exit(1)
// logging with fields can be used with any of the above
log.WithFields(log.F("key", "value")).Info("test info")
}
Adding your own Handler
package main
import (
"bytes"
"fmt"
"github.com/go-playground/log"
)
// CustomHandler is your custom handler
type CustomHandler struct {
// whatever properties you need
buffer uint // channel buffer
}
// Run starts the logger consuming on the returned channed
func (c *CustomHandler) Run() chan<- *log.Entry {
// in a big high traffic app, set a higher buffer
ch := make(chan *log.Entry, c.buffer)
// can run as many consumers on the channel as you want,
// depending on the buffer size or your needs
go func(entries <-chan *log.Entry) {
// below prints to os.Stderr but could marshal to JSON
// and send to central logging server
var e *log.Entry
b := new(bytes.Buffer)
for e = range entries {
b.Reset()
b.WriteString(e.Message)
for _, f := range e.Fields {
fmt.Fprintf(b, " %s=%v", f.Key, f.Value)
}
fmt.Println(b.String())
e.WG.Done() // done writing the entry
}
}(ch)
return ch
}
func main() {
cLog := &CustomHandler{
buffer: 0,
}
log.RegisterHandler(cLog, log.AllLevels...)
// Trace
defer log.Trace("trace").End()
log.Debug("debug")
log.Info("info")
log.Notice("notice")
log.Warn("warn")
log.Error("error")
// log.Panic("panic") // this will panic
log.Alert("alert")
// log.Fatal("fatal") // this will call os.Exit(1)
// logging with fields can be used with any of the above
log.WithFields(log.F("key", "value")).Info("test info")
}
Log Level Definitions
DebugLevel - Info useful to developers for debugging the application, not useful during
operations.
TraceLevel - Info useful to developers for debugging the application and reporting on
possible bottlenecks.
InfoLevel - Normal operational messages - may be harvested for reporting, measuring
throughput, etc. - no action required.
NoticeLevel - Normal but significant condition. Events that are unusual but not error
conditions - might be summarized in an email to developers or admins to
spot potential problems - no immediate action required.
WarnLevel - Warning messages, not an error, but indication that an error will occur if
action is not taken, e.g. file system 85% full - each item must be resolved
within a given time.
ErrorLevel - Non-urgent failures, these should be relayed to developers or admins; each
item must be resolved within a given time.
PanicLevel - A "panic" condition usually affecting multiple apps/servers/sites. At this
level it would usually notify all tech staff on call.
AlertLevel - Action must be taken immediately. Should be corrected immediately,
therefore notify staff who can fix the problem. An example would be the
loss of a primary ISP connection.
FatalLevel - Should be corrected immediately, but indicates failure in a primary
system, an example is a loss of a backup ISP connection.
( same as SYSLOG CRITICAL )
*/
package log