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

57 lines
1.2 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
}
2022-07-25 15:28:17 +00:00
// InitLogger initializes a global logger using an specific encoding
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")
}