trpc.group/trpc-go/trpc-go@v1.0.3/log/no_option_logger_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log_test
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"strconv"
    20  
    21  	"go.uber.org/zap"
    22  	"go.uber.org/zap/zapcore"
    23  
    24  	"trpc.group/trpc-go/trpc-go/internal/report"
    25  	"trpc.group/trpc-go/trpc-go/log"
    26  )
    27  
    28  // newNoOptionBufLogger creates a no option buf Logger from zap.
    29  func newNoOptionBufLogger(buf *bytes.Buffer, skip int) log.Logger {
    30  	encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
    31  	core := zapcore.NewCore(encoder, zapcore.AddSync(buf), zapcore.DebugLevel)
    32  	return &noOptionLog{
    33  		levels: []zap.AtomicLevel{},
    34  		logger: zap.New(
    35  			core,
    36  			zap.AddCallerSkip(skip),
    37  			zap.AddCaller(),
    38  		),
    39  	}
    40  }
    41  
    42  // noOptionLog is a log.Logger implementation based on zaplogger, but without option.
    43  type noOptionLog struct {
    44  	levels []zap.AtomicLevel
    45  	logger *zap.Logger
    46  }
    47  
    48  // With add user defined fields to log.Logger. Fields support multiple values.
    49  func (l *noOptionLog) With(fields ...log.Field) log.Logger {
    50  	zapFields := make([]zap.Field, len(fields))
    51  	for i := range fields {
    52  		zapFields[i] = zap.Any(fields[i].Key, fields[i].Value)
    53  	}
    54  
    55  	return &noOptionLog{
    56  		levels: l.levels,
    57  		logger: l.logger.With(zapFields...)}
    58  }
    59  
    60  func getLogMsg(args ...interface{}) string {
    61  	msg := fmt.Sprint(args...)
    62  	report.LogWriteSize.IncrBy(float64(len(msg)))
    63  	return msg
    64  }
    65  
    66  func getLogMsgf(format string, args ...interface{}) string {
    67  	msg := fmt.Sprintf(format, args...)
    68  	report.LogWriteSize.IncrBy(float64(len(msg)))
    69  	return msg
    70  }
    71  
    72  // Trace logs to TRACE log. Arguments are handled in the manner of fmt.Print.
    73  func (l *noOptionLog) Trace(args ...interface{}) {
    74  	if l.logger.Core().Enabled(zapcore.DebugLevel) {
    75  		l.logger.Debug(getLogMsg(args...))
    76  	}
    77  }
    78  
    79  // Tracef logs to TRACE log. Arguments are handled in the manner of fmt.Printf.
    80  func (l *noOptionLog) Tracef(format string, args ...interface{}) {
    81  	if l.logger.Core().Enabled(zapcore.DebugLevel) {
    82  		l.logger.Debug(getLogMsgf(format, args...))
    83  	}
    84  }
    85  
    86  // Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
    87  func (l *noOptionLog) Debug(args ...interface{}) {
    88  	if l.logger.Core().Enabled(zapcore.DebugLevel) {
    89  		l.logger.Debug(getLogMsg(args...))
    90  	}
    91  }
    92  
    93  // Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
    94  func (l *noOptionLog) Debugf(format string, args ...interface{}) {
    95  	if l.logger.Core().Enabled(zapcore.DebugLevel) {
    96  		l.logger.Debug(getLogMsgf(format, args...))
    97  	}
    98  }
    99  
   100  // Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
   101  func (l *noOptionLog) Info(args ...interface{}) {
   102  	if l.logger.Core().Enabled(zapcore.InfoLevel) {
   103  		l.logger.Info(getLogMsg(args...))
   104  	}
   105  }
   106  
   107  // Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
   108  func (l *noOptionLog) Infof(format string, args ...interface{}) {
   109  	if l.logger.Core().Enabled(zapcore.InfoLevel) {
   110  		l.logger.Info(getLogMsgf(format, args...))
   111  	}
   112  }
   113  
   114  // Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
   115  func (l *noOptionLog) Warn(args ...interface{}) {
   116  	if l.logger.Core().Enabled(zapcore.WarnLevel) {
   117  		l.logger.Warn(getLogMsg(args...))
   118  	}
   119  }
   120  
   121  // Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
   122  func (l *noOptionLog) Warnf(format string, args ...interface{}) {
   123  	if l.logger.Core().Enabled(zapcore.WarnLevel) {
   124  		l.logger.Warn(getLogMsgf(format, args...))
   125  	}
   126  }
   127  
   128  // Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
   129  func (l *noOptionLog) Error(args ...interface{}) {
   130  	if l.logger.Core().Enabled(zapcore.ErrorLevel) {
   131  		l.logger.Error(getLogMsg(args...))
   132  	}
   133  }
   134  
   135  // Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
   136  func (l *noOptionLog) Errorf(format string, args ...interface{}) {
   137  	if l.logger.Core().Enabled(zapcore.ErrorLevel) {
   138  		l.logger.Error(getLogMsgf(format, args...))
   139  	}
   140  }
   141  
   142  // Fatal logs to FATAL log. Arguments are handled in the manner of fmt.Print.
   143  func (l *noOptionLog) Fatal(args ...interface{}) {
   144  	if l.logger.Core().Enabled(zapcore.FatalLevel) {
   145  		l.logger.Fatal(getLogMsg(args...))
   146  	}
   147  }
   148  
   149  // Fatalf logs to FATAL log. Arguments are handled in the manner of fmt.Printf.
   150  func (l *noOptionLog) Fatalf(format string, args ...interface{}) {
   151  	if l.logger.Core().Enabled(zapcore.FatalLevel) {
   152  		l.logger.Fatal(getLogMsgf(format, args...))
   153  	}
   154  }
   155  
   156  // Sync calls the zap logger's Sync method, and flushes any buffered log entries.
   157  // Applications should take care to call Sync before exiting.
   158  func (l *noOptionLog) Sync() error {
   159  	return l.logger.Sync()
   160  }
   161  
   162  // SetLevel sets output log level.
   163  func (l *noOptionLog) SetLevel(output string, level log.Level) {
   164  	i, e := strconv.Atoi(output)
   165  	if e != nil {
   166  		return
   167  	}
   168  	if i < 0 || i >= len(l.levels) {
   169  		return
   170  	}
   171  	l.levels[i].SetLevel(levelToZapLevel[level])
   172  }
   173  
   174  // GetLevel gets output log level.
   175  func (l *noOptionLog) GetLevel(output string) log.Level {
   176  	i, e := strconv.Atoi(output)
   177  	if e != nil {
   178  		return log.LevelDebug
   179  	}
   180  	if i < 0 || i >= len(l.levels) {
   181  		return log.LevelDebug
   182  	}
   183  	return zapLevelToLevel[l.levels[i].Level()]
   184  }
   185  
   186  var levelToZapLevel = map[log.Level]zapcore.Level{
   187  	log.LevelTrace: zapcore.DebugLevel,
   188  	log.LevelDebug: zapcore.DebugLevel,
   189  	log.LevelInfo:  zapcore.InfoLevel,
   190  	log.LevelWarn:  zapcore.WarnLevel,
   191  	log.LevelError: zapcore.ErrorLevel,
   192  	log.LevelFatal: zapcore.FatalLevel,
   193  }
   194  
   195  var zapLevelToLevel = map[zapcore.Level]log.Level{
   196  	zapcore.DebugLevel: log.LevelDebug,
   197  	zapcore.InfoLevel:  log.LevelInfo,
   198  	zapcore.WarnLevel:  log.LevelWarn,
   199  	zapcore.ErrorLevel: log.LevelError,
   200  	zapcore.FatalLevel: log.LevelFatal,
   201  }