Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
Gordon 2023-03-10 21:05:27 +08:00
commit 0b39dd9097
2 changed files with 12 additions and 113 deletions

View File

@ -24,8 +24,7 @@ func NewRootCmd() (rootCmd *RootCmd) {
if err != nil { if err != nil {
return err return err
} }
log.InitFromConfig("newlog") return log.InitFromConfig("newlog")
return nil
}, },
} }
rootCmd.Command = c rootCmd.Command = c

View File

@ -7,39 +7,22 @@ import (
"context" "context"
"time" "time"
"github.com/go-logr/logr"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
var ( var (
discardLogger = logr.Discard() pkgLogger Logger = &ZapLogger{}
defaultLogger Logger = LogRLogger(discardLogger)
pkgLogger Logger = LogRLogger(discardLogger)
) )
// InitFromConfig initializes a Zap-based logger // InitFromConfig initializes a Zap-based logger
func InitFromConfig(name string) { func InitFromConfig(name string) error {
//var c zap.Config
//file, _ := os.Create(config.Config.Log.StorageLocation)
//writeSyncer := zapcore.AddSync(file)
l, err := NewZapLogger() l, err := NewZapLogger()
if err == nil { if err != nil {
setLogger(l, name) return err
} }
}
// GetLogger returns the logger that was set with SetLogger with an extra depth of 1
func GetLogger() Logger {
return defaultLogger
}
// SetLogger lets you use a custom logger. Pass in a logr.Logger with default depth
func setLogger(l Logger, name string) {
defaultLogger = l.WithCallDepth(1).WithName(name)
// pkg wrapper needs to drop two levels of depth
pkgLogger = l.WithCallDepth(2).WithName(name) pkgLogger = l.WithCallDepth(2).WithName(name)
return nil
} }
func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) { func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
@ -58,14 +41,6 @@ func Error(ctx context.Context, msg string, err error, keysAndValues ...interfac
pkgLogger.Error(ctx, msg, err, keysAndValues...) pkgLogger.Error(ctx, msg, err, keysAndValues...)
} }
func ParseZapLevel(level string) zapcore.Level {
lvl := zapcore.InfoLevel
if level != "" {
_ = lvl.UnmarshalText([]byte(level))
}
return lvl
}
type Logger interface { type Logger interface {
Debug(ctx context.Context, msg string, keysAndValues ...interface{}) Debug(ctx context.Context, msg string, keysAndValues ...interface{})
Info(ctx context.Context, msg string, keysAndValues ...interface{}) Info(ctx context.Context, msg string, keysAndValues ...interface{})
@ -94,8 +69,13 @@ func NewZapLogger() (*ZapLogger, error) {
Development: true, Development: true,
Encoding: "json", Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(), EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{config.Config.Log.StorageLocation}, OutputPaths: []string{"stdout", config.Config.Log.StorageLocation + "openIM2.log"},
ErrorOutputPaths: []string{config.Config.Log.StorageLocation}, ErrorOutputPaths: []string{config.Config.Log.StorageLocation},
Sampling: &zap.SamplingConfig{
Initial: 0,
Thereafter: 0,
Hook: nil,
},
} }
l, err := zapConfig.Build() l, err := zapConfig.Build()
if err != nil { if err != nil {
@ -103,35 +83,8 @@ func NewZapLogger() (*ZapLogger, error) {
} }
zl := &ZapLogger{ zl := &ZapLogger{
unsampled: l.Sugar(), unsampled: l.Sugar(),
//SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second,
//SampleInitial: conf.ItemSampleInitial,
//SampleInterval: conf.ItemSampleInterval,
} }
//if conf.Sample {
// // use a sampling logger for the main logger
// samplingConf := &zap.SamplingConfig{
// Initial: conf.SampleInitial,
// Thereafter: conf.SampleInterval,
// }
// // sane defaults
// if samplingConf.Initial == 0 {
// samplingConf.Initial = 20
// }
// if samplingConf.Thereafter == 0 {
// samplingConf.Thereafter = 100
// }
// zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
// return zapcore.NewSamplerWithOptions(
// core,
// time.Second,
// samplingConf.Initial,
// samplingConf.Thereafter,
// )
// })).Sugar()
//} else {
// zl.zap = zl.unsampled
//}
return zl, nil return zl, nil
} }
@ -168,7 +121,6 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger { func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
dup := *l dup := *l
dup.zap = l.zap.With(keysAndValues...) dup.zap = l.zap.With(keysAndValues...)
// mirror unsampled logger too
if l.unsampled == l.zap { if l.unsampled == l.zap {
dup.unsampled = dup.zap dup.unsampled = dup.zap
} else { } else {
@ -223,55 +175,3 @@ func (l *ZapLogger) WithoutSampler() Logger {
dup.zap = l.unsampled dup.zap = l.unsampled
return &dup return &dup
} }
type LogRLogger logr.Logger
func (l LogRLogger) toLogr() logr.Logger {
if logr.Logger(l).GetSink() == nil {
return discardLogger
}
return logr.Logger(l)
}
func (l LogRLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
l.toLogr().V(1).Info(msg, keysAndValues...)
}
func (l LogRLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
l.toLogr().Info(msg, keysAndValues...)
}
func (l LogRLogger) Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
if err != nil {
keysAndValues = append(keysAndValues, "error", err)
}
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
l.toLogr().Info(msg, keysAndValues...)
}
func (l LogRLogger) Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
l.toLogr().Error(err, msg, keysAndValues...)
}
func (l LogRLogger) WithValues(keysAndValues ...interface{}) Logger {
return LogRLogger(l.toLogr().WithValues(keysAndValues...))
}
func (l LogRLogger) WithName(name string) Logger {
return LogRLogger(l.toLogr().WithName(name))
}
func (l LogRLogger) WithCallDepth(depth int) Logger {
return LogRLogger(l.toLogr().WithCallDepth(depth))
}
func (l LogRLogger) WithItemSampler() Logger {
return l
}
func (l LogRLogger) WithoutSampler() Logger {
return l
}