-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
124 lines (102 loc) · 2.74 KB
/
logger.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
package go_logger
import (
env "github.com/fabiotavarespr/go-env"
"sync"
"github.com/fabiotavarespr/go-logger/attributes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Option struct {
ServiceName string
ServiceVersion string
Environment string
LogLevel string
}
var (
once sync.Once
skipper = zap.AddCallerSkip(1)
option *Option
)
func Init(opt *Option) {
once.Do(func() {
option = opt
zapLogger, err := newZap()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)
})
}
func Sync() error {
return zap.L().Sync()
}
func Debug(message string, attr attributes.Attributes) {
zap.L().WithOptions(skipper).Debug(message, makeAttributes(attr)...)
}
func Info(message string, attr attributes.Attributes) {
zap.L().WithOptions(skipper).Info(message, makeAttributes(attr)...)
}
func Warn(message string, attr attributes.Attributes) {
zap.L().WithOptions(skipper).Warn(message, makeAttributes(attr)...)
}
func Fatal(message string, attr attributes.Attributes) {
zap.L().WithOptions(skipper).Fatal(message, makeAttributes(attr)...)
}
func Error(message string, attr attributes.Attributes) {
zap.L().WithOptions(skipper).Error(message, makeAttributes(attr)...)
}
func newZap(options ...zap.Option) (*zap.Logger, error) {
return newZapConfig().Build(options...)
}
func newZapConfig() zap.Config {
logLevel := zap.NewAtomicLevel()
err := logLevel.UnmarshalText([]byte(option.LogLevel))
if err != nil {
panic(err)
}
log := zap.Config{
Level: logLevel,
Development: true,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: newZapEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
if env.GetEnvWithDefaultAsString("ENVIRONMENT", "prod") == "prod" {
log.Development = false
}
return log
}
func newZapEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
TimeKey: "Timestamp",
LevelKey: "Severity",
NameKey: "Logger",
CallerKey: "Caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "Message",
StacktraceKey: "Stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
}
func makeAttributes(attr attributes.Attributes) []zapcore.Field {
if attr == nil {
attr = attributes.New()
}
return []zapcore.Field{
zap.Any("Attributes", attr),
zap.Any("Resource", map[string]interface{}{
"service.name": option.ServiceName,
"service.version": option.ServiceVersion,
"service.environment": option.Environment,
}),
}
}