Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log/slog: export Source method in Record for custom handler support #70281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/log/slog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/log/slog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/log/slog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
4 changes: 2 additions & 2 deletions src/log/slog/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion src/log/slog/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
}
Expand Down