/
logger.go
executable file
·156 lines (125 loc) · 3.76 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package logger
import (
"os"
"github.com/cafasaru/microservice/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger methods interface
type Logger interface {
InitLogger()
Debug(args ...interface{})
Debugf(template string, args ...interface{})
Info(args ...interface{})
Infof(template string, args ...interface{})
Warn(args ...interface{})
Warnf(template string, args ...interface{})
Error(args ...interface{})
Errorf(template string, args ...interface{})
DPanic(args ...interface{})
DPanicf(template string, args ...interface{})
Fatal(args ...interface{})
Fatalf(template string, args ...interface{})
Printf(template string, args ...interface{})
}
// Logger
type apiLogger struct {
cfg *config.Config
sugarLogger *zap.SugaredLogger
}
// NewApiLogger App Logger constructor
func NewApiLogger(cfg *config.Config) *apiLogger {
return &apiLogger{cfg: cfg}
}
// For mapping config logger to email_service logger levels
var loggerLevelMap = map[string]zapcore.Level{
"debug": zapcore.DebugLevel,
"info": zapcore.InfoLevel,
"warn": zapcore.WarnLevel,
"error": zapcore.ErrorLevel,
"dpanic": zapcore.DPanicLevel,
"panic": zapcore.PanicLevel,
"fatal": zapcore.FatalLevel,
}
func (l *apiLogger) getLoggerLevel(cfg *config.Config) zapcore.Level {
level, exist := loggerLevelMap[cfg.Logger.Level]
if !exist {
return zapcore.DebugLevel
}
return level
}
// InitLogger Init logger
func (l *apiLogger) InitLogger() {
logLevel := l.getLoggerLevel(l.cfg)
logWriter := zapcore.AddSync(os.Stderr)
var encoderCfg zapcore.EncoderConfig
if l.cfg.HTTP.Development {
encoderCfg = zap.NewDevelopmentEncoderConfig()
} else {
encoderCfg = zap.NewProductionEncoderConfig()
}
var encoder zapcore.Encoder
encoderCfg.LevelKey = "LEVEL"
encoderCfg.CallerKey = "CALLER"
encoderCfg.TimeKey = "TIME"
encoderCfg.NameKey = "NAME"
encoderCfg.MessageKey = "MESSAGE"
if l.cfg.Logger.Encoding == "console" {
encoder = zapcore.NewConsoleEncoder(encoderCfg)
} else {
encoder = zapcore.NewJSONEncoder(encoderCfg)
}
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
core := zapcore.NewCore(encoder, logWriter, zap.NewAtomicLevelAt(logLevel))
logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
l.sugarLogger = logger.Sugar()
if err := l.sugarLogger.Sync(); err != nil {
l.sugarLogger.Error(err)
}
}
// Logger methods
func (l *apiLogger) Debug(args ...interface{}) {
l.sugarLogger.Debug(args...)
}
func (l *apiLogger) Debugf(template string, args ...interface{}) {
l.sugarLogger.Debugf(template, args...)
}
func (l *apiLogger) Info(args ...interface{}) {
l.sugarLogger.Info(args...)
}
func (l *apiLogger) Infof(template string, args ...interface{}) {
l.sugarLogger.Infof(template, args...)
}
func (l *apiLogger) Printf(template string, args ...interface{}) {
l.sugarLogger.Infof(template, args...)
}
func (l *apiLogger) Warn(args ...interface{}) {
l.sugarLogger.Warn(args...)
}
func (l *apiLogger) Warnf(template string, args ...interface{}) {
l.sugarLogger.Warnf(template, args...)
}
func (l *apiLogger) Error(args ...interface{}) {
l.sugarLogger.Error(args...)
}
func (l *apiLogger) Errorf(template string, args ...interface{}) {
l.sugarLogger.Errorf(template, args...)
}
func (l *apiLogger) DPanic(args ...interface{}) {
l.sugarLogger.DPanic(args...)
}
func (l *apiLogger) DPanicf(template string, args ...interface{}) {
l.sugarLogger.DPanicf(template, args...)
}
func (l *apiLogger) Panic(args ...interface{}) {
l.sugarLogger.Panic(args...)
}
func (l *apiLogger) Panicf(template string, args ...interface{}) {
l.sugarLogger.Panicf(template, args...)
}
func (l *apiLogger) Fatal(args ...interface{}) {
l.sugarLogger.Fatal(args...)
}
func (l *apiLogger) Fatalf(template string, args ...interface{}) {
l.sugarLogger.Fatalf(template, args...)
}