/
main.go
90 lines (73 loc) · 1.98 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Package main contains examples of how to use the logger package
package main
import (
"errors"
"fmt"
"github.com/phamtai97/go-utils/utils/logger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type user struct {
Username string
Age int
}
func (u *user) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("name", u.Username)
enc.AddInt("age", u.Age)
return nil
}
func main() {
// Can customize the logger config
// cfg := logger.Config{
// Level: logger.INFO,
// FileLogConfig: logger.FileLogConfig{
// IsUseFile: true,
// FilePath: "./logs.log",
// },
// }
// New default config
// cfg := logger.NewDefaultConfig()
// New production config, write logs to file
// cfg := logger.NewProductionConfig(true, "./logs.log")
// New production config, write logs to console
// cfg := logger.NewProductionConfig("")
// Can init logger with simple line
// logger.InitProduction("./logs.log")
cfg := logger.Config{
Level: logger.DEBUG,
}
if err := logger.Init(cfg); err != nil {
fmt.Printf("Failed to init logger: %v\n", err)
}
defer logger.Sync()
logger.Debug("Test debug logger",
zap.String("Hey, ", "I am a software engineer"),
zap.Object("My information: ", &user{
Username: "AJPham",
Age: 1997,
}))
logger.Info("Test info logger",
zap.String("Hey, ", "I am a software engineer"),
zap.Object("My information: ", &user{
Username: "AJPham",
Age: 1997,
}))
logger.Error("Test error logger",
zap.String("Hey, ", "I am a software engineer"),
zap.Object("My information: ", &user{
Username: "AJPham",
Age: 1997,
}), zap.Error(errors.New("Failed to write log")))
logger.Warn("Test warn logger",
zap.String("Hey, ", "I am a software engineer"),
zap.Object("My information: ", &user{
Username: "AJPham",
Age: 1997,
}))
logger.Fatal("Test fatal logger",
zap.String("Hey, ", "I am a software engineer"),
zap.Object("My information: ", &user{
Username: "AJPham",
Age: 1997,
}))
}