-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
67 lines (59 loc) · 1.7 KB
/
main.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
package main
import "github.com/kataras/golog"
func main() {
golog.SetLevel("debug")
golog.SetFormat("json", " ") // < --
// To register a custom formatter:
// golog.RegisterFormatter(golog.Formatter...)
/* Example Output:
{
"timestamp": 1591423477,
"level": "debug",
"message": "This is a message with data (debug prints the stacktrace too)",
"fields": {
"username": "kataras"
},
"stacktrace": [
{
"function": "main.main",
"source": "C:/mygopath/src/github.com/kataras/golog/_examples/customize-output/main.go:29"
}
]
}
*/
golog.Debugf("This is a %s with data (debug prints the stacktrace too)", "message", golog.Fields{
"username": "kataras",
}) // If more than one golog.Fields passed, then they are merged into a single map.
/* Example Output:
{
"timestamp": 1591423477,
"level": "info",
"message": "An info message",
"fields": {
"home": "https://iris-go.com"
}
"stacktrace": [...]
}
*/
golog.Infof("An info message", golog.Fields{"home": "https://iris-go.com"})
golog.Warnf("Hey, warning here")
golog.Errorf("Something went wrong!")
// You can also pass custom structs, like normally you would do.
type myCustomData struct {
Username string `json:"username"`
Email string `json:"email"`
}
golog.Fatalf("A fatal error for %s screen!", "home", golog.Fields{"data": myCustomData{
Username: "kataras",
Email: "kataras2006@hotmail.com",
}})
}
/* Manually, use it for any custom format:
golog.Handle(jsonOutput)
func jsonOutput(l *golog.Log) bool {
enc := json.NewEncoder(l.Logger.Printer)
enc.SetIndent("", " ")
err := enc.Encode(l)
return err == nil
}
*/