From 9121c71a6e4496069bbb2b6c5961609f29f209fa Mon Sep 17 00:00:00 2001 From: nesty92 Date: Mon, 11 Nov 2024 09:32:03 +0000 Subject: [PATCH] log/slog: export Source method in Record for custom handler support Currently, the `source` method in `slog.Record` is not accessible to custom handlers, requiring developers to re-implement logic for retrieving source location information. This commit exports the `source` method as `Source`, enabling consistent access for custom logging handlers and reducing code redundancy. Fixes #70280 Change-Id: I3eb3bc60658abc5de95697a10bddd11ab54c6e13 GitHub-Last-Rev: 5c37e88ee57badccef0dfcac4337cf34848c7484 GitHub-Pull-Request: golang/go#70281 --- src/log/slog/handler.go | 2 +- src/log/slog/handler_test.go | 2 +- src/log/slog/logger_test.go | 2 +- src/log/slog/record.go | 4 ++-- src/log/slog/record_test.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/log/slog/handler.go b/src/log/slog/handler.go index 2ff85b582ef32..f9c9518f3f17f 100644 --- a/src/log/slog/handler.go +++ b/src/log/slog/handler.go @@ -294,7 +294,7 @@ func (h *commonHandler) handle(r Record) error { } // source if h.opts.AddSource { - state.appendAttr(Any(SourceKey, r.source())) + state.appendAttr(Any(SourceKey, r.Source())) } key = MessageKey msg := r.Message diff --git a/src/log/slog/handler_test.go b/src/log/slog/handler_test.go index 8ce34526d0ba4..e2ab68209444c 100644 --- a/src/log/slog/handler_test.go +++ b/src/log/slog/handler_test.go @@ -530,7 +530,7 @@ func TestJSONAndTextHandlers(t *testing.T) { }, } { r := NewRecord(testTime, LevelInfo, "message", callerPC(2)) - line := strconv.Itoa(r.source().Line) + line := strconv.Itoa(r.Source().Line) r.AddAttrs(test.attrs...) var buf bytes.Buffer opts := HandlerOptions{ReplaceAttr: test.replace, AddSource: test.addSource} diff --git a/src/log/slog/logger_test.go b/src/log/slog/logger_test.go index 9efd4ed0e91f1..dff2f71b07a51 100644 --- a/src/log/slog/logger_test.go +++ b/src/log/slog/logger_test.go @@ -185,7 +185,7 @@ func TestCallDepth(t *testing.T) { const wantFunc = "log/slog.TestCallDepth" const wantFile = "logger_test.go" wantLine := startLine + count*2 - got := h.r.source() + got := h.r.Source() gotFile := filepath.Base(got.File) if got.Function != wantFunc || gotFile != wantFile || got.Line != wantLine { t.Errorf("got (%s, %s, %d), want (%s, %s, %d)", diff --git a/src/log/slog/record.go b/src/log/slog/record.go index 97c87019a6aff..5a06f4f8fd657 100644 --- a/src/log/slog/record.go +++ b/src/log/slog/record.go @@ -211,11 +211,11 @@ func (s *Source) group() Value { return GroupValue(as...) } -// source returns a Source for the log event. +// Source returns a Source for the log event. // If the Record was created without the necessary information, // or if the location is unavailable, it returns a non-nil *Source // with zero fields. -func (r Record) source() *Source { +func (r Record) Source() *Source { fs := runtime.CallersFrames([]uintptr{r.PC}) f, _ := fs.Next() return &Source{ diff --git a/src/log/slog/record_test.go b/src/log/slog/record_test.go index 931ab660418db..575c7221f0ba6 100644 --- a/src/log/slog/record_test.go +++ b/src/log/slog/record_test.go @@ -56,7 +56,7 @@ func TestRecordSource(t *testing.T) { pc = callerPC(test.depth + 1) } r := NewRecord(time.Time{}, 0, "", pc) - got := r.source() + got := r.Source() if i := strings.LastIndexByte(got.File, '/'); i >= 0 { got.File = got.File[i+1:] }