diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index d27c51091..3f2057c40 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -24,8 +24,7 @@ func NewRootCmd() (rootCmd *RootCmd) { if err != nil { return err } - log.InitFromConfig("newlog") - return nil + return log.InitFromConfig("newlog") }, } rootCmd.Command = c diff --git a/pkg/common/logger/log.go b/pkg/common/logger/log.go index 0b3d12cad..15cd6d14a 100644 --- a/pkg/common/logger/log.go +++ b/pkg/common/logger/log.go @@ -7,39 +7,22 @@ import ( "context" "time" - "github.com/go-logr/logr" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var ( - discardLogger = logr.Discard() - defaultLogger Logger = LogRLogger(discardLogger) - pkgLogger Logger = LogRLogger(discardLogger) + pkgLogger Logger = &ZapLogger{} ) // InitFromConfig initializes a Zap-based logger -func InitFromConfig(name string) { - //var c zap.Config - //file, _ := os.Create(config.Config.Log.StorageLocation) - //writeSyncer := zapcore.AddSync(file) - +func InitFromConfig(name string) error { l, err := NewZapLogger() - if err == nil { - setLogger(l, name) + if err != nil { + 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) + return nil } 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...) } -func ParseZapLevel(level string) zapcore.Level { - lvl := zapcore.InfoLevel - if level != "" { - _ = lvl.UnmarshalText([]byte(level)) - } - return lvl -} - type Logger interface { Debug(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, Encoding: "json", EncoderConfig: zap.NewProductionEncoderConfig(), - OutputPaths: []string{config.Config.Log.StorageLocation}, + OutputPaths: []string{"stdout", config.Config.Log.StorageLocation + "openIM2.log"}, ErrorOutputPaths: []string{config.Config.Log.StorageLocation}, + Sampling: &zap.SamplingConfig{ + Initial: 0, + Thereafter: 0, + Hook: nil, + }, } l, err := zapConfig.Build() if err != nil { @@ -103,35 +83,8 @@ func NewZapLogger() (*ZapLogger, error) { } zl := &ZapLogger{ 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 } @@ -168,7 +121,6 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger { dup := *l dup.zap = l.zap.With(keysAndValues...) - // mirror unsampled logger too if l.unsampled == l.zap { dup.unsampled = dup.zap } else { @@ -223,55 +175,3 @@ func (l *ZapLogger) WithoutSampler() Logger { dup.zap = l.unsampled 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 -}