2022-01-18 18:17:06 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-06-01 20:03:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log *zap.Logger = nil
|
|
|
|
var atom = zap.NewAtomicLevel()
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// SetLogLevel sets a custom log level
|
2022-01-18 18:17:06 +00:00
|
|
|
func SetLogLevel(level string) error {
|
|
|
|
lvl := zapcore.InfoLevel // zero value
|
|
|
|
err := lvl.Set(level)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
atom.SetLevel(lvl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// Logger creates a zap.Logger with some reasonable defaults
|
2022-05-27 13:25:06 +00:00
|
|
|
func Logger() *zap.Logger {
|
2022-01-18 18:17:06 +00:00
|
|
|
if log == nil {
|
2022-06-01 20:03:26 +00:00
|
|
|
InitLogger("console")
|
2022-01-18 18:17:06 +00:00
|
|
|
}
|
2022-05-27 13:25:06 +00:00
|
|
|
return log
|
2022-01-18 18:17:06 +00:00
|
|
|
}
|
2022-06-01 20:03:26 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|