diff --git a/pkg/log/config.go b/pkg/log/config.go index c79ca54..769deb5 100644 --- a/pkg/log/config.go +++ b/pkg/log/config.go @@ -23,6 +23,7 @@ var ( loggerFileMapMutex sync.Mutex ) +// Cfg is the log config type Cfg struct { Level string `yaml:"level"` File string `yaml:"file"` @@ -44,6 +45,7 @@ func init() { Config(v) } +// Config for log configuration func Config(v *viper.Viper) { if err := v.UnmarshalKey("log", &cfg); err != nil { Fatal("parse log config", zap.Error(err)) @@ -66,6 +68,7 @@ func Config(v *viper.Viper) { logger = newLogger(zaplogger) } +// NewLoggerFile create a new logger with file and use the global log configuration func NewLogger(file string) (Logger, error) { c := cfg if c.File == file { diff --git a/pkg/log/echo_logger.go b/pkg/log/echo_logger.go index 9688777..2032b27 100644 --- a/pkg/log/echo_logger.go +++ b/pkg/log/echo_logger.go @@ -10,131 +10,163 @@ import ( "github.com/labstack/gommon/log" ) +// EchoLogger echo logger +// see echo.Logger type EchoLogger struct { prefix string logger Logger } +// NewEchoLogger crate new echo log instance func NewEchoLogger() *EchoLogger { return &EchoLogger{ logger: Named(""), } } +// For with context.Context func (t *EchoLogger) For(ctx context.Context) *EchoLogger { return &EchoLogger{prefix: t.prefix, logger: t.logger.For(ctx)} } +// Output get output func (t *EchoLogger) Output() io.Writer { return nil } +// SetOutput not implemented, ignored func (t *EchoLogger) SetOutput(w io.Writer) {} +// Prefix get prefix func (t *EchoLogger) Prefix() string { return t.prefix } +// SetOutput set output prefix func (t *EchoLogger) SetPrefix(p string) { t.prefix = p t.logger = Named(p) } +// Level get log level func (t *EchoLogger) Level() log.Lvl { return log.INFO } +// SetLvl set log level func (t *EchoLogger) SetLevel(v log.Lvl) {} +// SetHeader set header, not implemented func (t *EchoLogger) SetHeader(h string) {} +// Print log func (t *EchoLogger) Print(i ...interface{}) { t.logger.Info(strings.TrimSpace(fmt.Sprintln(i...))) } +// Printf log formatted func (t *EchoLogger) Printf(format string, args ...interface{}) { t.logger.Info(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Printj log json formatted func (t *EchoLogger) Printj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Info(strings.TrimSpace(string(b))) } +// Debug log debug message func (t *EchoLogger) Debug(i ...interface{}) { t.logger.Debug(strings.TrimSpace(fmt.Sprintln(i...))) } +// Debugf log debug formatted message func (t *EchoLogger) Debugf(format string, args ...interface{}) { t.logger.Debug(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Debugj log json formatted func (t *EchoLogger) Debugj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Debug(strings.TrimSpace(string(b))) } +// Info log func (t *EchoLogger) Info(i ...interface{}) { t.logger.Info(strings.TrimSpace(fmt.Sprintln(i...))) } +// Infof log formatted func (t *EchoLogger) Infof(format string, args ...interface{}) { t.logger.Info(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Infoj log json func (t *EchoLogger) Infoj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Info(strings.TrimSpace(string(b))) } +// Warn log warn message func (t *EchoLogger) Warn(i ...interface{}) { t.logger.Warn(strings.TrimSpace(fmt.Sprintln(i...))) } +// Warnf log warn formatted message func (t *EchoLogger) Warnf(format string, args ...interface{}) { t.logger.Warn(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Warnj log warn json func (t *EchoLogger) Warnj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Warn(strings.TrimSpace(string(b))) } +// Error log error message func (t *EchoLogger) Error(i ...interface{}) { t.logger.Error(strings.TrimSpace(fmt.Sprintln(i...))) } +// Errorf log error formatted message func (t *EchoLogger) Errorf(format string, args ...interface{}) { t.logger.Error(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Errorj log error json func (t *EchoLogger) Errorj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Error(strings.TrimSpace(string(b))) } +// Fatal log fatal message func (t *EchoLogger) Fatal(i ...interface{}) { t.logger.Error(strings.TrimSpace(fmt.Sprintln(i...))) } +// Fatalj log fatal json message func (t *EchoLogger) Fatalj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Error(strings.TrimSpace(string(b))) } +// Fatalf log formatted message func (t *EchoLogger) Fatalf(format string, args ...interface{}) { t.logger.Error(strings.TrimSpace(fmt.Sprintf(format, args...))) } +// Panic log panic message, which will cause panic func (t *EchoLogger) Panic(i ...interface{}) { t.logger.Fatal(strings.TrimSpace(fmt.Sprintln(i...))) } +// Panicj log panic json message, which will cause panic func (t *EchoLogger) Panicj(j log.JSON) { b, _ := json.Marshal(j) t.logger.Fatal(strings.TrimSpace(string(b))) } +// Panicf log formatted message, which will cause panic func (t *EchoLogger) Panicf(format string, args ...interface{}) { t.logger.Fatal(strings.TrimSpace(fmt.Sprintf(format, args...))) } diff --git a/pkg/log/log.go b/pkg/log/log.go index c935c69..6b12063 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -6,38 +6,48 @@ import ( "go.uber.org/zap" ) +// Named name log +// see zap log named func Named(name string) Logger { return logger.Named(name) } +// Debug log message func Debug(msg string, fields ...zap.Field) { logger.Debug(msg, fields...) } +// Info log message func Info(msg string, fields ...zap.Field) { logger.Info(msg, fields...) } +// Warn log message func Warn(msg string, fields ...zap.Field) { logger.Warn(msg, fields...) } +// Error log message func Error(msg string, fields ...zap.Field) { logger.Error(msg, fields...) } +// Fatal log message func Fatal(msg string, fields ...zap.Field) { logger.Fatal(msg, fields...) } +// With log message func With(fields ...zap.Field) Logger { return logger.With(fields...) } +// For log with context.Context, which will log trace_id and span_id if opentracing enabled func For(ctx context.Context) Logger { return logger.For(ctx) } +// Get get zap log instance (global instance only) func Get() *zap.Logger { return zaplogger } diff --git a/pkg/log/logger.go b/pkg/log/logger.go index dbbf2de..1120019 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -9,24 +9,44 @@ import ( "go.uber.org/zap/zapcore" ) +// Logger log interface definition type Logger interface { + // Named see zap Named Named(name string) Logger + + // Debug log message Debug(msg string, fields ...zapcore.Field) + + // Info log message Info(msg string, fields ...zapcore.Field) + + // Warn log message Warn(msg string, fields ...zapcore.Field) + + // Error log message Error(msg string, fields ...zapcore.Field) + + // Fatal log message Fatal(msg string, fields ...zapcore.Field) + + // With add zap fields With(fields ...zapcore.Field) Logger + + // For log with context.Context, which will log trace_id and span_id if opentracing enabled For(ctx context.Context) Logger + + // CallerSkip skip caller for adjust caller CallerSkip(int) Logger } +// default logger implementation type defaultLogger struct { logger *zap.Logger additionalFields []zapcore.Field skiped bool } +// newLogger new logger instance func newLogger(l *zap.Logger) Logger { return &defaultLogger{ logger: l, @@ -34,31 +54,38 @@ func newLogger(l *zap.Logger) Logger { } } +// Named see zap Named func (t defaultLogger) Named(name string) Logger { t.logger = t.logger.Named(name) return t } +// Debug log message func (t defaultLogger) Debug(msg string, fields ...zapcore.Field) { t.logger.Debug(msg, fields...) } +// Info log message func (t defaultLogger) Info(msg string, fields ...zapcore.Field) { t.logger.Info(msg, fields...) } +// Warn log message func (t defaultLogger) Warn(msg string, fields ...zapcore.Field) { t.logger.Warn(msg, fields...) } +// Error log message func (t defaultLogger) Error(msg string, fields ...zapcore.Field) { t.logger.Error(msg, fields...) } +// Fatal log message func (t defaultLogger) Fatal(msg string, fields ...zapcore.Field) { t.logger.Fatal(msg, fields...) } +// With add zap fields func (t defaultLogger) With(fields ...zapcore.Field) Logger { return defaultLogger{ logger: t.logger.WithOptions(zap.AddCallerSkip(t.skip())).With(fields...), @@ -67,6 +94,7 @@ func (t defaultLogger) With(fields ...zapcore.Field) Logger { } } +// For log with context.Context, which will log trace_id and span_id if opentracing enabled func (t defaultLogger) For(ctx context.Context) Logger { if span := opentracing.SpanFromContext(ctx); span != nil { l := spanLogger{span: span, logger: t.logger.WithOptions(zap.AddCallerSkip(t.skip())), additionalFields: t.additionalFields} @@ -84,6 +112,7 @@ func (t defaultLogger) For(ctx context.Context) Logger { return defaultLogger{logger: t.logger.WithOptions(zap.AddCallerSkip(-1))} } +// CallerSkip skip caller for adjust caller func (t defaultLogger) CallerSkip(skip int) Logger { t.logger = t.logger.WithOptions(zap.AddCallerSkip(skip)) return t diff --git a/pkg/log/spanlogger.go b/pkg/log/spanlogger.go index ddb2dc1..6d93bfb 100644 --- a/pkg/log/spanlogger.go +++ b/pkg/log/spanlogger.go @@ -20,11 +20,14 @@ type spanLogger struct { additionalFields []zapcore.Field } +// Named name log +// see zap log named func (t spanLogger) Named(name string) Logger { t.logger = t.logger.Named(name) return t } +// Debug log message func (t spanLogger) Debug(msg string, fields ...zapcore.Field) { if t.logger.Core().Enabled(zap.DebugLevel) { t.logToSpan("debug", msg, append(t.additionalFields, fields...)...) @@ -32,6 +35,7 @@ func (t spanLogger) Debug(msg string, fields ...zapcore.Field) { t.logger.Debug(msg, append(t.spanFields, fields...)...) } +// Info log message func (t spanLogger) Info(msg string, fields ...zapcore.Field) { if t.logger.Core().Enabled(zap.InfoLevel) { t.logToSpan("info", msg, append(t.additionalFields, fields...)...) @@ -39,23 +43,27 @@ func (t spanLogger) Info(msg string, fields ...zapcore.Field) { t.logger.Info(msg, append(t.spanFields, fields...)...) } +// Warn log message func (t spanLogger) Warn(msg string, fields ...zapcore.Field) { t.logToSpan("warn", msg, append(t.additionalFields, fields...)...) t.logger.Warn(msg, append(t.spanFields, fields...)...) } +// Error log message func (t spanLogger) Error(msg string, fields ...zapcore.Field) { t.logToSpan("error", msg, fields...) ext.Error.Set(t.span, true) t.logger.Error(msg, append(t.spanFields, fields...)...) } +// Fatal log message func (t spanLogger) Fatal(msg string, fields ...zapcore.Field) { t.logToSpan("fatal", msg, append(t.additionalFields, fields...)...) ext.Error.Set(t.span, true) t.logger.Fatal(msg, append(t.spanFields, fields...)...) } +// With add zap fields func (t spanLogger) With(fields ...zapcore.Field) Logger { return spanLogger{ logger: t.logger.With(fields...), @@ -65,10 +73,12 @@ func (t spanLogger) With(fields ...zapcore.Field) Logger { } } +// For log with context.Context, which will log trace_id and span_id if opentracing enabled func (t spanLogger) For(context.Context) Logger { return t } +// CallerSkip skip caller for adjust caller func (t spanLogger) CallerSkip(skip int) Logger { t.logger = t.logger.WithOptions(zap.AddCallerSkip(skip)) return t