From da45c89e4ec51d8dd7db211e3016318f86ac8745 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 14 Oct 2023 18:59:27 +0300 Subject: [PATCH] logger: slog improve Signed-off-by: Vasiliy Tolstov --- logger/logger.go | 90 +++++++++++++----------------------------------- logger/slog.go | 62 ++++++++------------------------- 2 files changed, 37 insertions(+), 115 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 7064bdbd..59a9c80b 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -32,33 +32,19 @@ type Logger interface { // Fields set fields to always be logged with keyval pairs Fields(fields ...interface{}) Logger // Info level message - Info(ctx context.Context, args ...interface{}) - // Trace level message - Trace(ctx context.Context, args ...interface{}) - // Debug level message - Debug(ctx context.Context, args ...interface{}) - // Warn level message - Warn(ctx context.Context, args ...interface{}) - // Error level message - Error(ctx context.Context, args ...interface{}) - // Fatal level message - Fatal(ctx context.Context, args ...interface{}) - // Infof level message - Infof(ctx context.Context, msg string, args ...interface{}) + Info(ctx context.Context, msg string, args ...interface{}) // Tracef level message - Tracef(ctx context.Context, msg string, args ...interface{}) + Trace(ctx context.Context, msg string, args ...interface{}) // Debug level message - Debugf(ctx context.Context, msg string, args ...interface{}) + Debug(ctx context.Context, msg string, args ...interface{}) // Warn level message - Warnf(ctx context.Context, msg string, args ...interface{}) + Warn(ctx context.Context, msg string, args ...interface{}) // Error level message - Errorf(ctx context.Context, msg string, args ...interface{}) + Error(ctx context.Context, msg string, args ...interface{}) // Fatal level message - Fatalf(ctx context.Context, msg string, args ...interface{}) + Fatal(ctx context.Context, msg string, args ...interface{}) // Log logs message with needed level - Log(ctx context.Context, level Level, args ...interface{}) - // Logf logs message with needed level - Logf(ctx context.Context, level Level, msg string, args ...interface{}) + Log(ctx context.Context, level Level, msg string, args ...interface{}) // String returns the name of logger String() string } @@ -66,64 +52,34 @@ type Logger interface { // Field contains keyval pair type Field interface{} -// Info writes msg to default logger on info level -func Info(ctx context.Context, args ...interface{}) { - DefaultLogger.Info(ctx, args...) +// Info writes formatted msg to default logger on info level +func Info(ctx context.Context, msg string, args ...interface{}) { + DefaultLogger.Info(ctx, msg, args...) } -// Error writes msg to default logger on error level -func Error(ctx context.Context, args ...interface{}) { - DefaultLogger.Error(ctx, args...) -} - -// Debug writes msg to default logger on debug level -func Debug(ctx context.Context, args ...interface{}) { - DefaultLogger.Debug(ctx, args...) -} - -// Warn writes msg to default logger on warn level -func Warn(ctx context.Context, args ...interface{}) { - DefaultLogger.Warn(ctx, args...) -} - -// Trace writes msg to default logger on trace level -func Trace(ctx context.Context, args ...interface{}) { - DefaultLogger.Trace(ctx, args...) -} - -// Fatal writes msg to default logger on fatal level -func Fatal(ctx context.Context, args ...interface{}) { - DefaultLogger.Fatal(ctx, args...) -} - -// Infof writes formatted msg to default logger on info level -func Infof(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Infof(ctx, msg, args...) -} - -// Errorf writes formatted msg to default logger on error level -func Errorf(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Errorf(ctx, msg, args...) +// Error writes formatted msg to default logger on error level +func Error(ctx context.Context, msg string, args ...interface{}) { + DefaultLogger.Error(ctx, msg, args...) } // Debugf writes formatted msg to default logger on debug level func Debugf(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Debugf(ctx, msg, args...) + DefaultLogger.Debug(ctx, msg, args...) } -// Warnf writes formatted msg to default logger on warn level -func Warnf(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Warnf(ctx, msg, args...) +// Warn writes formatted msg to default logger on warn level +func Warn(ctx context.Context, msg string, args ...interface{}) { + DefaultLogger.Warn(ctx, msg, args...) } -// Tracef writes formatted msg to default logger on trace level -func Tracef(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Tracef(ctx, msg, args...) +// Trace writes formatted msg to default logger on trace level +func Trace(ctx context.Context, msg string, args ...interface{}) { + DefaultLogger.Trace(ctx, msg, args...) } -// Fatalf writes formatted msg to default logger on fatal level -func Fatalf(ctx context.Context, msg string, args ...interface{}) { - DefaultLogger.Fatalf(ctx, msg, args...) +// Fatal writes formatted msg to default logger on fatal level +func Fatal(ctx context.Context, msg string, args ...interface{}) { + DefaultLogger.Fatal(ctx, msg, args...) } // V returns true if passed level enabled in default logger diff --git a/logger/slog.go b/logger/slog.go index 5031900a..8fb4554e 100644 --- a/logger/slog.go +++ b/logger/slog.go @@ -2,7 +2,6 @@ package logger import ( "context" - "fmt" "os" "go.unistack.org/micro/v4/options" @@ -133,70 +132,37 @@ func (s *slogLogger) Init(opts ...options.Option) error { return nil } -func (s *slogLogger) Log(ctx context.Context, lvl Level, args ...any) { +func (s *slogLogger) Log(ctx context.Context, lvl Level, msg string, args ...any) { if !s.V(lvl) { return } slvl := loggerToSlogLevel(lvl) - msg := fmt.Sprint(args...) - s.slog.Log(ctx, slvl, msg) + s.slog.Log(ctx, slvl, msg, args...) } -func (s *slogLogger) Logf(ctx context.Context, lvl Level, format string, args ...any) { - if !s.V(lvl) { - return - } - slvl := loggerToSlogLevel(lvl) - s.slog.Log(ctx, slvl, format, args...) +func (s *slogLogger) Info(ctx context.Context, msg string, args ...interface{}) { + s.Log(ctx, InfoLevel, msg, args...) } -func (s *slogLogger) Info(ctx context.Context, args ...any) { - s.Log(ctx, InfoLevel, args...) +func (s *slogLogger) Debug(ctx context.Context, msg string, args ...any) { + s.Log(ctx, DebugLevel, msg, args...) } -func (s *slogLogger) Infof(ctx context.Context, format string, args ...interface{}) { - s.Logf(ctx, InfoLevel, format, args...) +func (s *slogLogger) Trace(ctx context.Context, msg string, args ...interface{}) { + s.Log(ctx, TraceLevel, msg, args...) } -func (s *slogLogger) Debug(ctx context.Context, args ...any) { - s.Log(ctx, DebugLevel, args...) +func (s *slogLogger) Error(ctx context.Context, msg string, args ...any) { + s.Log(ctx, ErrorLevel, msg, args...) } -func (s *slogLogger) Debugf(ctx context.Context, format string, args ...any) { - s.Logf(ctx, DebugLevel, format, args...) -} - -func (s *slogLogger) Error(ctx context.Context, args ...any) { - s.Log(ctx, ErrorLevel, args...) -} - -func (s *slogLogger) Trace(ctx context.Context, args ...interface{}) { - s.Log(ctx, TraceLevel, args...) -} - -func (s *slogLogger) Tracef(ctx context.Context, msg string, args ...interface{}) { - s.Logf(ctx, TraceLevel, msg, args...) -} - -func (s *slogLogger) Errorf(ctx context.Context, format string, args ...any) { - s.Logf(ctx, ErrorLevel, format, args...) -} - -func (s *slogLogger) Fatal(ctx context.Context, args ...any) { - s.Log(ctx, FatalLevel, args...) -} - -func (s *slogLogger) Fatalf(ctx context.Context, msg string, args ...interface{}) { - s.Logf(ctx, FatalLevel, msg, args...) +func (s *slogLogger) Fatal(ctx context.Context, msg string, args ...interface{}) { + s.Log(ctx, FatalLevel, msg, args...) os.Exit(1) } -func (s *slogLogger) Warn(ctx context.Context, args ...any) { - s.Log(ctx, WarnLevel, args...) -} - -func (s *slogLogger) Warnf(ctx context.Context, format string, args ...any) { - s.Logf(ctx, WarnLevel, format, args...) +func (s *slogLogger) Warn(ctx context.Context, msg string, args ...any) { + s.Log(ctx, WarnLevel, msg, args...) } func (s *slogLogger) String() string {