go-waku/waku/v2/utils/logger.go

56 lines
1.1 KiB
Go
Raw Normal View History

package utils
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var log *zap.Logger = nil
var atom = zap.NewAtomicLevel()
// SetLogLevel sets a custom log level
func SetLogLevel(level string) error {
lvl := zapcore.InfoLevel // zero value
err := lvl.Set(level)
if err != nil {
return err
}
atom.SetLevel(lvl)
return nil
}
// Logger creates a zap.Logger with some reasonable defaults
2022-05-27 13:25:06 +00:00
func Logger() *zap.Logger {
if log == nil {
InitLogger("console")
}
2022-05-27 13:25:06 +00:00
return log
}
func InitLogger(encoding string) {
cfg := zap.Config{
Encoding: encoding,
Level: atom,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
NameKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, err := cfg.Build()
if err != nil {
panic(fmt.Errorf("could not create logger: %s", err.Error()))
}
log = logger.Named("gowaku")
}