Skip to content

Commit 994f4be

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add good docs
1 parent ae252e0 commit 994f4be

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

doc.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Package log - is a simple, highly configurable, structured logging that is a near drop in replacement for the std library log.
3+
4+
5+
Usage
6+
7+
package main
8+
9+
import (
10+
"github.com/go-playground/log"
11+
"github.com/go-playground/log/handlers/console"
12+
)
13+
14+
func main() {
15+
16+
cLog := console.New()
17+
18+
log.RegisterHandler(cLog, log.AllLevels...)
19+
20+
// Trace
21+
defer log.Trace("trace").End()
22+
23+
log.Debug("debug")
24+
log.Info("info")
25+
log.Notice("notice")
26+
log.Warn("warn")
27+
log.Error("error")
28+
// log.Panic("panic") // this will panic
29+
log.Alert("alert")
30+
// log.Fatal("fatal") // this will call os.Exit(1)
31+
32+
// logging with fields can be used with any of the above
33+
log.WithFields(log.F("key", "value")).Info("test info")
34+
}
35+
36+
Adding your own Handler
37+
38+
package main
39+
40+
import (
41+
"bytes"
42+
"fmt"
43+
44+
"github.com/go-playground/log"
45+
)
46+
47+
// CustomHandler is your custom handler
48+
type CustomHandler struct {
49+
// whatever properties you need
50+
buffer uint // channel buffer
51+
}
52+
53+
// Run starts the logger consuming on the returned channed
54+
func (c *CustomHandler) Run() chan<- *log.Entry {
55+
56+
// in a big high traffic app, set a higher buffer
57+
ch := make(chan *log.Entry, c.buffer)
58+
59+
// can run as many consumers on the channel as you want,
60+
// depending on the buffer size or your needs
61+
go func(entries <-chan *log.Entry) {
62+
63+
// below prints to os.Stderr but could marshal to JSON
64+
// and send to central logging server
65+
var e *log.Entry
66+
b := new(bytes.Buffer)
67+
68+
for e = range entries {
69+
70+
b.Reset()
71+
b.WriteString(e.Message)
72+
73+
for _, f := range e.Fields {
74+
fmt.Fprintf(b, " %s=%v", f.Key, f.Value)
75+
}
76+
77+
fmt.Println(b.String())
78+
e.WG.Done() // done writing the entry
79+
}
80+
81+
}(ch)
82+
83+
return ch
84+
}
85+
86+
func main() {
87+
88+
cLog := &CustomHandler{
89+
buffer: 0,
90+
}
91+
92+
log.RegisterHandler(cLog, log.AllLevels...)
93+
94+
// Trace
95+
defer log.Trace("trace").End()
96+
97+
log.Debug("debug")
98+
log.Info("info")
99+
log.Notice("notice")
100+
log.Warn("warn")
101+
log.Error("error")
102+
// log.Panic("panic") // this will panic
103+
log.Alert("alert")
104+
// log.Fatal("fatal") // this will call os.Exit(1)
105+
106+
// logging with fields can be used with any of the above
107+
log.WithFields(log.F("key", "value")).Info("test info")
108+
}
109+
110+
Log Level Definitions
111+
112+
DebugLevel - Info useful to developers for debugging the application, not useful during operations.
113+
114+
TraceLevel - Info useful to developers for debugging the application and reporting on possible bottlenecks.
115+
116+
InfoLevel - Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required.
117+
118+
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.
119+
120+
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.
121+
122+
ErrorLevel - Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time.
123+
124+
PanicLevel - A "panic" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call.
125+
126+
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.
127+
128+
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 )
129+
*/
130+
package log

0 commit comments

Comments
 (0)