github.com/GuanceCloud/cliutils@v1.1.21/logger/logger_ctx.go (about)

     1  package logger
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"go.uber.org/zap"
     8  )
     9  
    10  type LoggerCtx interface {
    11  	Debugf(template string, args ...interface{})
    12  	Infof(template string, args ...interface{})
    13  	Warnf(template string, args ...interface{})
    14  	Errorf(template string, args ...interface{})
    15  	Debug(args ...interface{})
    16  	Info(args ...interface{})
    17  	Warn(args ...interface{})
    18  	Error(args ...interface{})
    19  
    20  	DebugfCtx(ctx context.Context, template string, args ...interface{})
    21  	InfofCtx(ctx context.Context, template string, args ...interface{})
    22  	WarnfCtx(ctx context.Context, template string, args ...interface{})
    23  	ErrorfCtx(ctx context.Context, template string, args ...interface{})
    24  	DebugCtx(ctx context.Context, template string, args ...interface{})
    25  	InfoCtx(ctx context.Context, template string, args ...interface{})
    26  	WarnCtx(ctx context.Context, template string, args ...interface{})
    27  	ErrorCtx(ctx context.Context, template string, args ...interface{})
    28  
    29  	Named(name string) LoggerCtx
    30  	With(fields ...zap.Field) LoggerCtx
    31  }
    32  
    33  var _ LoggerCtx = (*loggerCtx)(nil)
    34  
    35  type loggerCtx struct {
    36  	logger *zap.Logger
    37  	config
    38  }
    39  
    40  func NewLoggerCtx(opts ...CtxOption) *loggerCtx {
    41  	cfg := newDefaultConfig()
    42  	for _, o := range opts {
    43  		cfg = o.apply(cfg)
    44  	}
    45  
    46  	cfg.opts = append(cfg.opts, zap.AddCallerSkip(1))
    47  
    48  	return &loggerCtx{
    49  		logger: zap.New(cfg.core, cfg.opts...),
    50  		config: cfg,
    51  	}
    52  }
    53  
    54  func (l *loggerCtx) GetTrace(ctx context.Context) Trace {
    55  	var traceResult Trace
    56  
    57  	if l.parseTrace != nil {
    58  		traceResult = l.parseTrace(ctx)
    59  	}
    60  
    61  	return traceResult
    62  }
    63  
    64  func (l *loggerCtx) getExtraFields(ctx context.Context) (fields []zap.Field) {
    65  	if l.enabledTrace {
    66  		trace := l.GetTrace(ctx)
    67  		fields = append(fields, zap.String(l.traceKey, trace.TraceID), zap.String(l.spanKey, trace.SpanID))
    68  	}
    69  	return fields
    70  }
    71  
    72  func (l *loggerCtx) getMessage(template string, args []interface{}) string {
    73  	if len(args) == 0 {
    74  		return template
    75  	}
    76  
    77  	if template != "" {
    78  		return fmt.Sprintf(template, args...)
    79  	}
    80  
    81  	if len(args) == 1 {
    82  		if str, ok := args[0].(string); ok {
    83  			return str
    84  		}
    85  	}
    86  	return fmt.Sprint(args...)
    87  }
    88  
    89  func (l *loggerCtx) Named(name string) LoggerCtx {
    90  	return &loggerCtx{
    91  		logger: l.logger.Named(name),
    92  		config: l.config,
    93  	}
    94  }
    95  
    96  func (l *loggerCtx) Debugf(template string, args ...interface{}) {
    97  	l.logger.Debug(l.getMessage(template, args))
    98  }
    99  
   100  func (l *loggerCtx) DebugfCtx(ctx context.Context, template string, args ...interface{}) {
   101  	l.logger.Debug(l.getMessage(template, args), l.getExtraFields(ctx)...)
   102  }
   103  
   104  func (l *loggerCtx) Infof(template string, args ...interface{}) {
   105  	l.logger.Info(l.getMessage(template, args))
   106  }
   107  
   108  func (l *loggerCtx) Warnf(template string, args ...interface{}) {
   109  	l.logger.Warn(l.getMessage(template, args))
   110  }
   111  
   112  func (l *loggerCtx) Errorf(template string, args ...interface{}) {
   113  	l.logger.Error(l.getMessage(template, args))
   114  }
   115  
   116  func (l *loggerCtx) InfofCtx(ctx context.Context, template string, args ...interface{}) {
   117  	l.logger.Info(l.getMessage(template, args), l.getExtraFields(ctx)...)
   118  }
   119  
   120  func (l *loggerCtx) WarnfCtx(ctx context.Context, template string, args ...interface{}) {
   121  	l.logger.Warn(l.getMessage(template, args), l.getExtraFields(ctx)...)
   122  }
   123  
   124  func (l *loggerCtx) ErrorfCtx(ctx context.Context, template string, args ...interface{}) {
   125  	l.logger.Error(l.getMessage(template, args), l.getExtraFields(ctx)...)
   126  }
   127  
   128  func (l *loggerCtx) Debug(args ...interface{}) {
   129  	l.logger.Debug(l.getMessage("", args))
   130  }
   131  
   132  func (l *loggerCtx) DebugCtx(ctx context.Context, template string, args ...interface{}) {
   133  	l.logger.Debug(l.getMessage(template, args), l.getExtraFields(ctx)...)
   134  }
   135  
   136  func (l *loggerCtx) Error(args ...interface{}) {
   137  	l.logger.Error(l.getMessage("", args))
   138  }
   139  
   140  func (l *loggerCtx) ErrorCtx(ctx context.Context, template string, args ...interface{}) {
   141  	l.logger.Error(l.getMessage(template, args), l.getExtraFields(ctx)...)
   142  }
   143  
   144  func (l *loggerCtx) Info(args ...interface{}) {
   145  	l.logger.Info(l.getMessage("", args))
   146  }
   147  
   148  func (l *loggerCtx) InfoCtx(ctx context.Context, template string, args ...interface{}) {
   149  	l.logger.Info(l.getMessage(template, args), l.getExtraFields(ctx)...)
   150  }
   151  
   152  func (l *loggerCtx) Warn(args ...interface{}) {
   153  	l.logger.Warn(l.getMessage("", args))
   154  }
   155  
   156  func (l *loggerCtx) WarnCtx(ctx context.Context, template string, args ...interface{}) {
   157  	l.logger.Warn(l.getMessage("", args), l.getExtraFields(ctx)...)
   158  }
   159  
   160  func (l *loggerCtx) With(fields ...zap.Field) LoggerCtx {
   161  	if len(fields) == 0 {
   162  		return l
   163  	}
   164  	temp := l.clone()
   165  	temp.logger = l.logger.With(fields...)
   166  	return temp
   167  }
   168  
   169  func (l *loggerCtx) clone() *loggerCtx {
   170  	return &loggerCtx{config: l.config}
   171  }