/
log.go
136 lines (109 loc) · 3.46 KB
/
log.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
package log
import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/caraml-dev/timber/common/config"
)
// Init the global logger to default prod settings. Calling InitGlobalLogger()
// will reset this.
var globalLogger = newDefaultGlobalLogger()
func newDefaultGlobalLogger() *zap.SugaredLogger {
logger, _ := zap.NewProduction()
return logger.Sugar()
}
// InitGlobalLogger creates a new SugaredLogger and assigns it as the global logger
func InitGlobalLogger(loglevel config.LogLevel) {
cfg := zap.NewProductionConfig()
// Disable annotation of logs with the calling function's file name and line number
cfg.DisableCaller = true
setLogLevel(&cfg, loglevel)
// Build logger
logger, _ := cfg.Build()
globalLogger = logger.Sugar()
}
// SetGlobalLogger takes a Logger instance as the input and sets it as the global
// logger, useful for testing.
func SetGlobalLogger(l *zap.SugaredLogger) {
globalLogger = l
}
// setLogLevel takes in a zap config and a LogLevel and sets the logging
// level in the config accordingly
func setLogLevel(cfg *zap.Config, logLvl config.LogLevel) {
var zapLevel zapcore.Level
switch logLvl {
case config.DebugLevel:
zapLevel = zap.DebugLevel
case config.WarnLevel:
zapLevel = zap.WarnLevel
case config.ErrorLevel:
zapLevel = zap.ErrorLevel
default:
// Use INFO by default
zapLevel = zapcore.InfoLevel
}
cfg.Level = zap.NewAtomicLevelAt(zapLevel)
}
// Info uses fmt.Println to log a message
func Info(args ...interface{}) {
globalLogger.Info(args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(template string, args ...interface{}) {
globalLogger.Infof(template, args...)
}
// Infow uses fmt.Sprintf to log a templated message.
func Infow(template string, args ...interface{}) {
globalLogger.Infow(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(template string, args ...interface{}) {
globalLogger.Warnf(template, args...)
}
// Warnw uses fmt.Sprintf to log a templated message.
func Warnw(template string, args ...interface{}) {
globalLogger.Warnw(template, args...)
}
// Error uses fmt.Println to log a message
func Error(args ...interface{}) {
globalLogger.Error(args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func Errorf(template string, args ...interface{}) {
globalLogger.Errorf(template, args...)
}
// Errorw uses fmt.Sprintf to log a templated message.
func Errorw(template string, args ...interface{}) {
globalLogger.Errorw(template, args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(template string, args ...interface{}) {
globalLogger.Debugf(template, args...)
}
// Debugw uses fmt.Sprintf to log a templated message.
func Debugw(template string, args ...interface{}) {
globalLogger.Debugw(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message.
func Fatalf(template string, args ...interface{}) {
globalLogger.Fatalf(template, args...)
}
// Panic uses fmt.Println to log a message
func Panic(args ...interface{}) {
globalLogger.Panic(args...)
}
// Panicf uses fmt.Sprintf to log a templated message.
func Panicf(template string, args ...interface{}) {
globalLogger.Panicf(template, args...)
}
// Panicw uses fmt.Sprintf to log a templated message.
func Panicw(template string, args ...interface{}) {
globalLogger.Panicw(template, args...)
}
// Sync uses fmt.Sprintf to log a templated message.
func Sync() error {
err := globalLogger.Sync()
if err != nil {
return err
}
return nil
}