api/pkg/logging/factory.go

66 lines
1.3 KiB
Go
Raw Normal View History

package logging
import (
"strings"
2020-12-30 16:03:01 +00:00
"testing"
"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
if initialFields != nil {
loggingConfig.InitialFields = initialFields
}
}
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)
}
}
func CreateLogger() (Logger, error) {
if zapLogger, err := loggingConfig.Build(); err != nil {
return nil, err
} else {
return NewLogger(zapLogger), nil
}
}
2020-12-30 16:03:01 +00:00
func CreateTestLogger(tb testing.TB) Logger {
return &testLogger{
tb: tb,
encoder: zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
}
}
func MustCreateLogger() Logger {
if logger, err := CreateLogger(); err != nil {
panic(err)
} else {
return logger
}
}