Add proper logging
This commit is contained in:
@@ -14,6 +14,8 @@ import (
|
||||
|
||||
"gitea.benny.dog/torjus/ezshare/store"
|
||||
"github.com/pelletier/go-toml"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"google.golang.org/grpc/credentials"
|
||||
)
|
||||
|
||||
@@ -310,3 +312,39 @@ func (sc *ServerUserStoreConfig) GetStore() (store.UserStore, func() error, erro
|
||||
|
||||
return nil, nil, fmt.Errorf("invalid store config")
|
||||
}
|
||||
|
||||
func (c *ServerConfig) GetLogger() *zap.SugaredLogger {
|
||||
logEncoderConfig := zap.NewProductionEncoderConfig()
|
||||
logEncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
logEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
logEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
logEncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
|
||||
|
||||
rootLoggerConfig := &zap.Config{
|
||||
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
||||
OutputPaths: []string{"stdout"},
|
||||
ErrorOutputPaths: []string{"stdout"},
|
||||
Encoding: "console",
|
||||
EncoderConfig: logEncoderConfig,
|
||||
DisableCaller: true,
|
||||
}
|
||||
|
||||
switch strings.ToUpper(c.LogLevel) {
|
||||
case "DEBUG":
|
||||
rootLoggerConfig.DisableCaller = false
|
||||
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
|
||||
case "INFO":
|
||||
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
case "WARN", "WARNING":
|
||||
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.WarnLevel)
|
||||
case "ERR", "ERROR":
|
||||
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel)
|
||||
}
|
||||
|
||||
rootLogger, err := rootLoggerConfig.Build()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return rootLogger.Sugar()
|
||||
}
|
||||
|
Reference in New Issue
Block a user