From 6dc7e792c8901483da82e6ff7633e13a050d036d Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 10 Nov 2020 22:18:16 +0300 Subject: [PATCH] logger improvements Signed-off-by: Vasiliy Tolstov --- logger/logger.go | 72 +++++++++++++++++++++++++++---------- logger/logger_test.go | 1 + logger/micro.go | 84 +++++++++++++++++++++++++++++++++---------- 3 files changed, 120 insertions(+), 37 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index e2d5711f..8e808653 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -17,43 +17,79 @@ type Logger interface { // Fields set fields to always be logged Fields(fields map[string]interface{}) Logger // Info level message - Info(msg string, args ...interface{}) + Info(args ...interface{}) // Trace level message - Trace(msg string, args ...interface{}) + Trace(args ...interface{}) // Debug level message - Debug(msg string, args ...interface{}) + Debug(args ...interface{}) // Warn level message - Warn(msg string, args ...interface{}) + Warn(args ...interface{}) // Error level message - Error(msg string, args ...interface{}) + Error(args ...interface{}) // Fatal level message - Fatal(msg string, args ...interface{}) + Fatal(args ...interface{}) + // Infof level message + Infof(msg string, args ...interface{}) + // Tracef level message + Tracef(msg string, args ...interface{}) + // Debug level message + Debugf(msg string, args ...interface{}) + // Warn level message + Warnf(msg string, args ...interface{}) + // Error level message + Errorf(msg string, args ...interface{}) + // Fatal level message + Fatalf(msg string, args ...interface{}) // String returns the name of logger String() string } -func Info(msg string, args ...interface{}) { - DefaultLogger.Info(msg, args...) +func Info(args ...interface{}) { + DefaultLogger.Info(args...) } -func Error(msg string, args ...interface{}) { - DefaultLogger.Error(msg, args...) +func Error(args ...interface{}) { + DefaultLogger.Error(args...) } -func Debug(msg string, args ...interface{}) { - DefaultLogger.Debug(msg, args...) +func Debug(args ...interface{}) { + DefaultLogger.Debug(args...) } -func Warn(msg string, args ...interface{}) { - DefaultLogger.Warn(msg, args...) +func Warn(args ...interface{}) { + DefaultLogger.Warn(args...) } -func Trace(msg string, args ...interface{}) { - DefaultLogger.Trace(msg, args...) +func Trace(args ...interface{}) { + DefaultLogger.Trace(args...) } -func Fatal(msg string, args ...interface{}) { - DefaultLogger.Fatal(msg, args...) +func Fatal(args ...interface{}) { + DefaultLogger.Fatal(args...) +} + +func Infof(msg string, args ...interface{}) { + DefaultLogger.Infof(msg, args...) +} + +func Errorf(msg string, args ...interface{}) { + DefaultLogger.Errorf(msg, args...) +} + +func Debugf(msg string, args ...interface{}) { + DefaultLogger.Debugf(msg, args...) +} + +func Warnf(msg string, args ...interface{}) { + DefaultLogger.Warnf(msg, args...) +} + +func Tracef(msg string, args ...interface{}) { + DefaultLogger.Tracef(msg, args...) +} + +func Fatalf(msg string, args ...interface{}) { + DefaultLogger.Fatalf(msg, args...) } func V(level Level) bool { diff --git a/logger/logger_test.go b/logger/logger_test.go index 10f0b0c2..02982f19 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -12,4 +12,5 @@ func TestLogger(t *testing.T) { l.Trace("trace_msg1") l.Warn("warn_msg1") l.Fields(map[string]interface{}{"error": "test"}).Info("error message") + l.Warn("first", " ", "second") } diff --git a/logger/micro.go b/logger/micro.go index d4b12c81..62c21161 100644 --- a/logger/micro.go +++ b/logger/micro.go @@ -84,32 +84,57 @@ func logCallerfilePath(loggingFilePath string) string { return loggingFilePath[idx+1:] } -func (l *defaultLogger) Info(msg string, args ...interface{}) { - l.log(InfoLevel, msg, args...) +func (l *defaultLogger) Info(args ...interface{}) { + l.log(InfoLevel, args...) } -func (l *defaultLogger) Error(msg string, args ...interface{}) { - l.log(ErrorLevel, msg, args...) +func (l *defaultLogger) Error(args ...interface{}) { + l.log(ErrorLevel, args...) } -func (l *defaultLogger) Debug(msg string, args ...interface{}) { - l.log(DebugLevel, msg, args...) +func (l *defaultLogger) Debug(args ...interface{}) { + l.log(DebugLevel, args...) } -func (l *defaultLogger) Warn(msg string, args ...interface{}) { - l.log(WarnLevel, msg, args...) +func (l *defaultLogger) Warn(args ...interface{}) { + l.log(WarnLevel, args...) } -func (l *defaultLogger) Trace(msg string, args ...interface{}) { - l.log(TraceLevel, msg, args...) +func (l *defaultLogger) Trace(args ...interface{}) { + l.log(TraceLevel, args...) } -func (l *defaultLogger) Fatal(msg string, args ...interface{}) { - l.log(FatalLevel, msg, args...) +func (l *defaultLogger) Fatal(args ...interface{}) { + l.log(FatalLevel, args...) os.Exit(1) } -func (l *defaultLogger) log(level Level, msg string, args ...interface{}) { +func (l *defaultLogger) Infof(msg string, args ...interface{}) { + l.logf(InfoLevel, msg, args...) +} + +func (l *defaultLogger) Errorf(msg string, args ...interface{}) { + l.logf(ErrorLevel, msg, args...) +} + +func (l *defaultLogger) Debugf(msg string, args ...interface{}) { + l.logf(DebugLevel, msg, args...) +} + +func (l *defaultLogger) Warnf(msg string, args ...interface{}) { + l.logf(WarnLevel, msg, args...) +} + +func (l *defaultLogger) Tracef(msg string, args ...interface{}) { + l.logf(TraceLevel, msg, args...) +} + +func (l *defaultLogger) Fatalf(msg string, args ...interface{}) { + l.logf(FatalLevel, msg, args...) + os.Exit(1) +} + +func (l *defaultLogger) log(level Level, args ...interface{}) { if !l.V(level) { return } @@ -125,12 +150,33 @@ func (l *defaultLogger) log(level Level, msg string, args ...interface{}) { } fields["timestamp"] = time.Now().Format("2006-01-02 15:04:05") - if len(msg) > 0 { - if len(args) > 0 { - fields["msg"] = fmt.Sprintf(msg, args...) - } else { - fields["msg"] = msg - } + fields["msg"] = fmt.Sprint(args...) + + l.RLock() + _ = l.enc.Encode(fields) + l.RUnlock() +} + +func (l *defaultLogger) logf(level Level, msg string, args ...interface{}) { + if !l.V(level) { + return + } + + l.RLock() + fields := copyFields(l.opts.Fields) + l.RUnlock() + + fields["level"] = level.String() + + if _, file, line, ok := runtime.Caller(l.opts.CallerSkipCount); ok { + fields["caller"] = fmt.Sprintf("%s:%d", logCallerfilePath(file), line) + } + + fields["timestamp"] = time.Now().Format("2006-01-02 15:04:05") + if len(args) > 0 { + fields["msg"] = fmt.Sprintf(msg, args...) + } else { + fields["msg"] = msg } l.RLock() _ = l.enc.Encode(fields)