github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/log/default_log.go (about)

     1  package log
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/weedge/lib/log/encoder"
     8  
     9  	"go.uber.org/zap"
    10  	"go.uber.org/zap/zapcore"
    11  )
    12  
    13  func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
    14  	enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
    15  }
    16  
    17  type consoleLog struct {
    18  	logger *zap.Logger
    19  }
    20  
    21  func newConsoleLogger() *consoleLog {
    22  	core := zapcore.NewCore(
    23  		encoder.NewSelfEncoder(zapcore.EncoderConfig{
    24  			//zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
    25  			// Keys can be anything except the empty string.
    26  			TimeKey:        "T",
    27  			LevelKey:       "L",
    28  			NameKey:        "N",
    29  			CallerKey:      "C",
    30  			MessageKey:     "M",
    31  			StacktraceKey:  "S",
    32  			LineEnding:     zapcore.DefaultLineEnding,
    33  			EncodeLevel:    zapcore.CapitalLevelEncoder,
    34  			EncodeTime:     TimeEncoder,
    35  			EncodeDuration: zapcore.StringDurationEncoder,
    36  			EncodeCaller:   zapcore.ShortCallerEncoder,
    37  		}),
    38  		zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout)),
    39  		zap.DebugLevel,
    40  	)
    41  	logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(2))
    42  	return &consoleLog{
    43  		logger: logger,
    44  	}
    45  }
    46  
    47  func (l *consoleLog) AccessInfo(msg string, fields ...zap.Field) {
    48  	l.logger.Info(msg, fields...)
    49  }
    50  
    51  func (l *consoleLog) Info(args ...interface{}) {
    52  	l.logger.Sugar().Info(args...)
    53  }
    54  func (l *consoleLog) Debug(args ...interface{}) {
    55  	l.logger.Sugar().Debug(args...)
    56  }
    57  func (l *consoleLog) Warn(args ...interface{}) {
    58  	l.logger.Sugar().Warn(args...)
    59  }
    60  func (l *consoleLog) Error(args ...interface{}) {
    61  	l.logger.Sugar().Error(args...)
    62  }
    63  func (l *consoleLog) Infof(format string, args ...interface{}) {
    64  	l.logger.Sugar().Infof(format, args...)
    65  }
    66  func (l *consoleLog) Debugf(format string, args ...interface{}) {
    67  	l.logger.Sugar().Debugf(format, args...)
    68  }
    69  func (l *consoleLog) Warnf(format string, args ...interface{}) {
    70  	l.logger.Sugar().Warnf(format, args...)
    71  }
    72  func (l *consoleLog) Errorf(format string, args ...interface{}) {
    73  	l.logger.Sugar().Errorf(format, args...)
    74  }
    75  func (l *consoleLog) FatalF(format string, args ...interface{}) {
    76  	l.logger.Sugar().Fatalf(format, args...)
    77  }
    78  func (l *consoleLog) Fatal(args ...interface{}) {
    79  	l.logger.Sugar().Fatal(args...)
    80  }