-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
175 lines (154 loc) · 4.91 KB
/
factory.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
package logger
import (
"os"
"strings"
"github.com/Sidddddarth/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
// default factory instance
var Default *Factory
func init() {
Default = NewFactory(config.Default)
}
// Reset resets the default logger factory.
// Shall only be used by tests, until we move to a proper DI framework
func Reset() {
Default = NewFactory(config.Default)
}
// NewFactory creates a new logger factory
func NewFactory(config *config.Config, options ...Option) *Factory {
f := &Factory{}
f.config = newConfig(config)
for _, option := range options {
option.apply(f)
}
f.zap = newZapLogger(config, f.config)
return f
}
// Factory is a factory for creating new loggers
type Factory struct {
config *factoryConfig
zap *zap.SugaredLogger
}
// NewLogger creates a new logger using the default logger factory
func NewLogger() Logger {
return Default.NewLogger()
}
// NewLogger creates a new logger
func (f *Factory) NewLogger() Logger {
return &logger{
logConfig: f.config,
zap: f.zap,
}
}
// GetLoggingConfig returns the log levels for default logger factory
func GetLoggingConfig() map[string]int {
return Default.GetLoggingConfig()
}
// GetLoggingConfig returns the log levels
func (f *Factory) GetLoggingConfig() map[string]int {
return f.config.levelConfigCache.m
}
// SetLogLevel sets the log level for a module for the default logger factory
func SetLogLevel(name, levelStr string) error {
return Default.SetLogLevel(name, levelStr)
}
// SetLogLevel sets the log level for a module
func (f *Factory) SetLogLevel(name, levelStr string) error {
err := f.config.SetLogLevel(name, levelStr)
if err != nil {
f.zap.Info(f.config.levelConfig)
}
return err
}
// Sync flushes the loggers' output buffers for the default logger factory
func Sync() {
Default.Sync()
}
// Sync flushes the loggers' output buffers
func (f *Factory) Sync() {
_ = f.zap.Sync()
}
func newConfig(config *config.Config) *factoryConfig {
fc := &factoryConfig{
levelConfig: &syncMap[string, int]{m: make(map[string]int)},
levelConfigCache: &syncMap[string, int]{m: make(map[string]int)},
}
fc.rootLevel = levelMap[config.GetString("LOG_LEVEL", "INFO")]
fc.enableNameInLog = config.GetBool("Logger.enableLoggerNameInLog", true)
config.RegisterBoolConfigVariable(false, &fc.enableStackTrace, true, "Logger.enableStackTrace")
config.GetBool("Logger.enableLoggerNameInLog", true)
// colon separated key value pairs
// Example: "router.GA=DEBUG:warehouse.REDSHIFT=DEBUG"
levelConfigStr := strings.TrimSpace(config.GetString("Logger.moduleLevels", ""))
if levelConfigStr != "" {
moduleLevelKVs := strings.Split(levelConfigStr, ":")
for _, moduleLevelKV := range moduleLevelKVs {
pair := strings.SplitN(moduleLevelKV, "=", 2)
if len(pair) < 2 {
continue
}
module := strings.TrimSpace(pair[0])
if module == "" {
continue
}
levelStr := strings.TrimSpace(pair[1])
level, ok := levelMap[levelStr]
if !ok {
continue
}
fc.levelConfig.set(module, level)
}
}
return fc
}
// newZapLogger configures the zap logger based on the config provide in config.toml
func newZapLogger(config *config.Config, fc *factoryConfig) *zap.SugaredLogger {
var cores []zapcore.Core
if config.GetBool("Logger.enableConsole", true) {
writer := zapcore.Lock(os.Stdout)
core := zapcore.NewCore(zapEncoder(config, config.GetBool("Logger.consoleJsonFormat", false)), writer, zapcore.DebugLevel)
cores = append(cores, core)
}
if config.GetBool("Logger.enableFile", false) {
writer := zapcore.AddSync(&lumberjack.Logger{
Filename: config.GetString("Logger.logFileLocation", "/tmp/rudder_log.log"),
MaxSize: config.GetInt("Logger.logFileSize", 100),
Compress: true,
LocalTime: true,
})
core := zapcore.NewCore(zapEncoder(config, config.GetBool("Logger.fileJsonFormat", false)), writer, zapcore.DebugLevel)
cores = append(cores, core)
}
combinedCore := zapcore.NewTee(cores...)
var options []zap.Option
if config.GetBool("Logger.enableFileNameInLog", true) {
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
}
if config.GetBool("Logger.enableStackTrace", false) {
// enables stack track for log level error
options = append(options, zap.AddStacktrace(zap.ErrorLevel))
}
if fc.clock != nil {
options = append(options, zap.WithClock(fc.clock))
}
zapLogger := zap.New(combinedCore, options...)
return zapLogger.Sugar()
}
// zapEncoder configures the output of the log
func zapEncoder(config *config.Config, json bool) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
if config.GetBool("Logger.enableTimestamp", true) {
encoderConfig.TimeKey = "ts"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
} else {
encoderConfig.TimeKey = ""
}
if json {
return zapcore.NewJSONEncoder(encoderConfig)
}
return zapcore.NewConsoleEncoder(encoderConfig)
}