From d4e6fb26f72380432082e49ea6895ffb30e5e305 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Oct 2023 01:04:59 +0300 Subject: [PATCH 1/2] logger: use names from opts Signed-off-by: Vasiliy Tolstov --- logger/slog.go | 15 +++++++++------ logger/wrapper/wrapper.go | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 logger/wrapper/wrapper.go diff --git a/logger/slog.go b/logger/slog.go index 8174a9e5..b0c87711 100644 --- a/logger/slog.go +++ b/logger/slog.go @@ -20,17 +20,20 @@ var ( fatalValue = slog.StringValue("fatal") ) -var renameAttr = func(_ []string, a slog.Attr) slog.Attr { +func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr { switch a.Key { case slog.SourceKey: source := a.Value.Any().(*slog.Source) a.Value = slog.StringValue(source.File + ":" + strconv.Itoa(source.Line)) - a.Key = "caller" + a.Key = s.opts.SourceKey case slog.TimeKey: - a.Key = "timestamp" + a.Key = s.opts.TimeKey + case slog.MessageKey: + a.Key = s.opts.MessageKey case slog.LevelKey: level := a.Value.Any().(slog.Level) lvl := slogToLoggerLevel(level) + a.Key = s.opts.LevelKey switch { case lvl < DebugLevel: a.Value = traceValue @@ -76,7 +79,7 @@ func (s *slogLogger) Clone(opts ...options.Option) Logger { l.leveler = new(slog.LevelVar) handleOpt := &slog.HandlerOptions{ - ReplaceAttr: renameAttr, + ReplaceAttr: s.renameAttr, Level: l.leveler, AddSource: true, } @@ -105,7 +108,7 @@ func (s *slogLogger) Attrs(attrs ...interface{}) Logger { nl.leveler.Set(s.leveler.Level()) handleOpt := &slog.HandlerOptions{ - ReplaceAttr: renameAttr, + ReplaceAttr: nl.renameAttr, Level: s.leveler, AddSource: true, } @@ -132,7 +135,7 @@ func (s *slogLogger) Init(opts ...options.Option) error { s.leveler = new(slog.LevelVar) handleOpt := &slog.HandlerOptions{ - ReplaceAttr: renameAttr, + ReplaceAttr: s.renameAttr, Level: s.leveler, AddSource: true, } diff --git a/logger/wrapper/wrapper.go b/logger/wrapper/wrapper.go new file mode 100644 index 00000000..c8f6f315 --- /dev/null +++ b/logger/wrapper/wrapper.go @@ -0,0 +1,4 @@ +package wrapper + +func NewWrapper(l logger.Logger) logger.Logger { +} -- 2.45.2 From a1ad690b7740de79f912d50f0fe360f1d436ceb3 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 17 Oct 2023 01:05:22 +0300 Subject: [PATCH 2/2] logger: use names from opts Signed-off-by: Vasiliy Tolstov --- logger/wrapper/wrapper.go | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 logger/wrapper/wrapper.go diff --git a/logger/wrapper/wrapper.go b/logger/wrapper/wrapper.go deleted file mode 100644 index c8f6f315..00000000 --- a/logger/wrapper/wrapper.go +++ /dev/null @@ -1,4 +0,0 @@ -package wrapper - -func NewWrapper(l logger.Logger) logger.Logger { -} -- 2.45.2