2020-04-01 02:08:21 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2020-12-26 13:11:49 +00:00
|
|
|
"strings"
|
2020-12-30 16:03:01 +00:00
|
|
|
"testing"
|
2020-12-26 13:11:49 +00:00
|
|
|
|
2020-04-01 02:08:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
loggingConfig = zap.NewProductionConfig()
|
|
|
|
)
|
|
|
|
|
|
|
|
func ConfigureLogging(
|
|
|
|
level zap.AtomicLevel,
|
|
|
|
developmentLogging bool,
|
|
|
|
initialFields map[string]interface{},
|
|
|
|
) {
|
|
|
|
loggingConfig.Level = level
|
|
|
|
loggingConfig.Development = developmentLogging
|
2020-04-13 22:14:56 +00:00
|
|
|
if initialFields != nil {
|
|
|
|
loggingConfig.InitialFields = initialFields
|
|
|
|
}
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseLevel(levelString string) zap.AtomicLevel {
|
|
|
|
switch strings.ToLower(levelString) {
|
|
|
|
case "debug":
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.DebugLevel)
|
|
|
|
case "info":
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.InfoLevel)
|
|
|
|
case "warn":
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.WarnLevel)
|
|
|
|
case "error":
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.ErrorLevel)
|
|
|
|
case "fatal":
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.FatalLevel)
|
|
|
|
default:
|
|
|
|
return zap.NewAtomicLevelAt(zapcore.InfoLevel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:14:56 +00:00
|
|
|
func CreateLogger() (Logger, error) {
|
|
|
|
if zapLogger, err := loggingConfig.Build(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return NewLogger(zapLogger), nil
|
|
|
|
}
|
2020-04-01 02:08:21 +00:00
|
|
|
}
|
2020-12-26 13:11:49 +00:00
|
|
|
|
2020-12-30 16:03:01 +00:00
|
|
|
func CreateTestLogger(tb testing.TB) Logger {
|
|
|
|
return &testLogger{
|
|
|
|
tb: tb,
|
|
|
|
encoder: zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func MustCreateLogger() Logger {
|
|
|
|
if logger, err := CreateLogger(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
}
|