diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 4803ea419..4d5c18d8b 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -13,7 +13,7 @@ import ( // log is A global variable so that log functions can be directly accessed var log Logger -var zl *zap.Logger +var logger *zap.Logger // Fields Type to pass when we want to call WithFields for structured logging type Fields map[string]interface{} @@ -32,6 +32,11 @@ type Logger interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) + // Fatal logs a message at Fatal level + // and process will exit with status set to 1. + Fatal(args ...interface{}) + Fatalf(format string, args ...interface{}) + WithFields(keyValues Fields) Logger } @@ -53,7 +58,7 @@ func Init(opts ...Option) Logger { } // new zap logger - zl, err = newZapLogger(cfg, opts...) + logger, err = newZapLogger(cfg, opts...) if err != nil { _ = fmt.Errorf("init newZapLogger err: %v", err) } @@ -74,7 +79,7 @@ func GetLogger() Logger { // GetZapLogger return raw zap logger func GetZapLogger() *zap.Logger { - return zl + return logger } // WithContext is a logger that can log msg and log span for trace @@ -82,7 +87,7 @@ func WithContext(ctx context.Context) Logger { //return zap logger if span := trace.SpanFromContext(ctx); span != nil { - logger := spanLogger{span: span, logger: zl} + logger := spanLogger{span: span, logger: logger} spanCtx := span.SpanContext() logger.spanFields = []zapcore.Field{ @@ -115,6 +120,11 @@ func Error(args ...interface{}) { log.Error(args...) } +// Fatal logger +func Fatal(args ...interface{}) { + log.Fatal(args...) +} + // Debugf logger func Debugf(format string, args ...interface{}) { log.Debugf(format, args...) @@ -135,6 +145,11 @@ func Errorf(format string, args ...interface{}) { log.Errorf(format, args...) } +// Fatalf logger +func Fatalf(format string, args ...interface{}) { + log.Fatalf(format, args...) +} + // WithFields logger // output more field, eg: // diff --git a/pkg/log/span_logger.go b/pkg/log/span_logger.go index e125e1648..2ed4390de 100644 --- a/pkg/log/span_logger.go +++ b/pkg/log/span_logger.go @@ -77,6 +77,22 @@ func (sl spanLogger) Errorf(format string, args ...interface{}) { sl.logger.Error(msg, append(sl.spanFields, fields...)...) } +func (sl spanLogger) Fatal(args ...interface{}) { + msg := fmt.Sprint(args...) + var fields []zap.Field + sl.logToSpan("error", msg) + sl.span.RecordError(errors.New(msg)) + sl.logger.Fatal(msg, append(sl.spanFields, fields...)...) +} + +func (sl spanLogger) Fatalf(format string, args ...interface{}) { + msg := fmt.Sprint(format, args) + var fields []zap.Field + sl.logToSpan("Errorf", msg) + sl.span.RecordError(errors.New(msg)) + sl.logger.Fatal(msg, append(sl.spanFields, fields...)...) +} + func (sl spanLogger) WithFields(keyValues Fields) Logger { panic("implement me") } diff --git a/pkg/log/zap.go b/pkg/log/zap.go index c344596da..717022c29 100644 --- a/pkg/log/zap.go +++ b/pkg/log/zap.go @@ -23,6 +23,9 @@ const ( logSuffix = ".log" warnLogSuffix = "_warn.log" errorLogSuffix = "_error.log" + + // defaultSkip zapLogger 包装了一层 zap.Logger,默认要跳过一层 + defaultSkip = 1 ) const ( @@ -32,8 +35,6 @@ const ( RotateTimeHourly = "hourly" ) -const defaultSkip = 1 // zapLogger 包装了一层 zap.Logger,默认要跳过一层 - var ( hostname string logDir string