-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
executable file
·148 lines (128 loc) · 3.67 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
package helpers
import (
"errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
LogLevelInfo = "info"
LogLevelDebug = "debug"
LogLevelError = "error"
LogFormatJSON = "json"
LogFormatText = "text"
)
var Logger = &logger{}
type logger struct {
}
var zapLogger *zap.Logger
func init() {
_ = InitLogger(LogLevelInfo, LogFormatJSON, false)
}
func InitLogger(loglevel, logFormat string, isDev bool) error {
var config zap.Config
if isDev {
config = zap.NewDevelopmentConfig()
if logFormat == LogFormatText {
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder // for setting color
}
} else {
config = zap.NewProductionConfig()
}
config.Encoding = getLogFormat(logFormat)
config.Level.SetLevel(getLogLevel(loglevel))
var err error
zapLogger, err = config.Build(zap.AddCallerSkip(1))
if err != nil {
return err
}
zap.RedirectStdLog(zapLogger)
return nil
}
func getLogFormat(logFormat string) string {
switch logFormat {
case LogFormatJSON:
return LogFormatJSON
case LogFormatText:
return "console"
default:
Logger.LogInfo(internalRequestID, "Invalid log format provided switching to log format json", nil)
return LogFormatJSON
}
}
func getLogLevel(logLevel string) zapcore.Level {
switch logLevel {
case LogLevelDebug:
return zap.DebugLevel
case LogLevelInfo:
return zap.InfoLevel
case LogLevelError:
return zap.DebugLevel
default:
Logger.LogInfo(internalRequestID, "Invalid log level provided switching to log level info", nil)
return zap.InfoLevel
}
}
// Error stores error info
type Error struct {
message string
rawError string
}
// NewError creates a new error object
func NewError(message, rawError string) Error {
return Error{message: message, rawError: rawError}
}
func (e Error) RawError() string {
return e.rawError
}
func (e Error) Error() string {
return e.message
}
// LogError logs the error in the proper format
func (l *logger) LogError(requestID, message string, err error, fields map[string]interface{}) error {
value, ok := err.(Error)
if ok {
return value
}
if err == nil {
err = errors.New("")
}
// Log the error
if fields != nil {
zapLogger.Error(message, zap.Any("error", err.Error()), zap.String("requestId", requestID), zap.Any("fields", fields))
} else {
zapLogger.Error(message, zap.Any("error", err.Error()), zap.String("requestId", requestID))
}
return Error{message: message, rawError: err.Error()}
}
// LogWarn logs the warning message in the proper format
func (l *logger) LogWarn(requestID, message string, fields map[string]interface{}) {
if fields != nil {
zapLogger.Warn(message, zap.String("requestId", requestID), zap.Any("fields", fields))
} else {
zapLogger.Warn(message, zap.String("requestId", requestID))
}
}
// LogInfo logs the info message in the proper format
func (l *logger) LogInfo(requestID, message string, fields map[string]interface{}) {
if fields != nil {
zapLogger.Info(message, zap.String("requestId", requestID), zap.Any("fields", fields))
} else {
zapLogger.Info(message, zap.String("requestId", requestID))
}
}
// LogDebug logs the debug message in proper format
func (l *logger) LogDebug(requestID, message string, fields map[string]interface{}) {
if fields != nil {
zapLogger.Debug(message, zap.String("requestId", requestID), zap.Any("fields", fields))
} else {
zapLogger.Debug(message, zap.String("requestId", requestID))
}
}
// LogFatal logs the fatal message in proper format
func (l *logger) LogFatal(requestID, message string, fields map[string]interface{}) {
if fields != nil {
zapLogger.Fatal(message, zap.String("requestId", requestID), zap.Any("fields", fields))
} else {
zapLogger.Fatal(message, zap.String("requestId", requestID))
}
}