/
logger.go
187 lines (147 loc) · 4.44 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package logger
import (
"errors"
"fmt"
"sync"
)
// A global variable so that log functions can be directly accessed
var log = DefaultLogger()
// Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}
// LoggerBackend reprents the int enum for backend of logger (exists for legacy compat reason)
// nolint:revive
type LoggerBackend int
const (
// Debug has verbose message
debugLvl = "debug"
// Info is default log level
infoLvl = "info"
// Warn is for logging messages about possible issues
warnLvl = "warn"
// Error is for logging errors
errorLvl = "error"
// Fatal is for logging fatal messages. The sytem shutsdown after logging the message.
fatalLvl = "fatal"
)
const (
// LoggerBackendZap logging using Uber's zap backend
LoggerBackendZap LoggerBackend = iota
// LoggerBackendLogrus logging using logrus backend
LoggerBackendLogrus
)
var (
errInvalidLoggerInstance = errors.New("invalid logger instance")
once sync.Once
)
// Logger is our contract for the logger
type Logger interface {
Debug(msg string)
Debugf(format string, args ...interface{})
Info(msg string)
Infof(format string, args ...interface{})
Warn(msg string)
Warnf(format string, args ...interface{})
Error(msg string)
Errorf(format string, args ...interface{})
Fatal(msg string)
Fatalf(format string, args ...interface{})
Panic(msg string)
Panicf(format string, args ...interface{})
WithFields(keyValues Fields) Logger
GetDelegate() interface{}
Sync() error
}
// Configuration stores the config for the logger
// For some loggers there can only be one level across writers, for such the level of Console is picked by default
type Configuration struct {
EnableConsole bool `name:"log-enable-console" help:"Enable log console" env:"LOG_ENABLE_CONSOLE" default:"true" yaml:"enable_console" mapstructure:"enable_console"`
ConsoleJSONFormat bool `name:"log-console-json-format" help:"Console to json format" env:"LOG_CONSOLE_JSON_FORMAT" default:"false" yaml:"console_log_format" mapstructure:"console_log_format"`
ConsoleLevel string `name:"log-console-level" help:"Console log level" env:"LOG_CONSOLE_LEVEL" default:"info" enum:"debug, info, warn, error, fatal, panic" yaml:"console_level" mapstructure:"console_level"`
EnableFile bool
FileJSONFormat bool
FileLevel string
FileLocation string
}
// DefaultLogger creates default logger, which uses zap sugarlogger and outputs to console
func DefaultLogger() Logger {
cfg := Configuration{
EnableConsole: true,
ConsoleJSONFormat: false,
ConsoleLevel: "info",
EnableFile: false,
FileJSONFormat: false,
}
logger, _ := newZapLogger(cfg)
return logger
}
// InitLogger returns an instance of logger
func InitLogger(config Configuration, backend LoggerBackend) (Logger, error) {
var err error
once.Do(func() {
switch backend {
case LoggerBackendZap:
log, err = NewLogger(config, backend)
case LoggerBackendLogrus:
log, err = NewLogger(config, backend)
default:
err = errInvalidLoggerInstance
}
})
return log, err
}
func NewLogger(config Configuration, backend LoggerBackend) (Logger, error) {
switch backend {
case LoggerBackendZap:
return newZapLogger(config)
case LoggerBackendLogrus:
return newLogrusLogger(config)
default:
return nil, errInvalidLoggerInstance
}
}
func Debug(msg string) {
log.Debugf(msg)
}
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
func Info(msg string) {
log.Infof(msg)
}
func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
func Warn(msg string) {
log.Warnf(msg)
}
func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}
func Error(msg string) {
fmt.Println(msg)
log.Errorf(msg)
}
func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
func Fatal(msg string) {
log.Fatalf(msg)
}
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func Panic(msg string) {
log.Panicf(msg)
}
func Panicf(format string, args ...interface{}) {
log.Panicf(format, args...)
}
func Sync() error {
return log.Sync()
}
func WithFields(keyValues Fields) Logger {
return log.WithFields(keyValues)
}
func GetDelegate() interface{} {
return log.GetDelegate()
}