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

proposal: log/slog: export Source method in Record to support custom handler implementations #70280

Open
nesty92 opened this issue Nov 11, 2024 · 6 comments · May be fixed by #70281
Open

proposal: log/slog: export Source method in Record to support custom handler implementations #70280

nesty92 opened this issue Nov 11, 2024 · 6 comments · May be fixed by #70281
Labels
Milestone

Comments

@nesty92
Copy link

nesty92 commented Nov 11, 2024

Go version

go version go1.23.1 linux/amd64

Output of go env in your module/workspace:

Not relevant

What did you do?

Implement a custom handler for the log/slog package. My custom handler requires access to the source location (file and line) of log entries for detailed logging.

What did you see happen?

The source method in slog.Record is not exported, meaning it’s inaccessible to custom handlers. As a result, I had to re-implement logic to retrieve source location information, which led to redundant code and reduced consistency with Go's built-in logging behavior.

What did you expect to see?

Expected an accessible Source method in slog.Record that allows custom handlers to retrieve source location information without re-implementing the existing internal logic. This would simplify the creation of custom handlers and ensure consistent behavior when accessing log entry sources.

@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

nesty92 added a commit to nesty92/go that referenced this issue Nov 11, 2024
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 golang#70280
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/626976 mentions this issue: log/slog: export Source method in Record for custom handler support

@seankhliao seankhliao changed the title log/slog: export Source method in Record to support custom handler implementations proposal: log/slog: export Source method in Record to support custom handler implementations Nov 11, 2024
@gopherbot gopherbot added this to the Proposal milestone Nov 11, 2024
@seankhliao
Copy link
Member

Do we need to export it when it's just a single call to runtime.CallersFrames?

@nesty92
Copy link
Author

nesty92 commented Nov 11, 2024

@seankhliao Yes, it's just a single call, but having it exported prevents each developer creating a custom handler from having to re-implement the same logic.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Nov 11, 2024
@ianlancetaylor
Copy link
Member

CC @jba

@jba
Copy link
Contributor

jba commented Nov 11, 2024

I can't think of a good reason not to export it. It's not much code, but it's also not code that we would expect most people to be comfortable writing. The fact that it's a method and not a field is enough of a clue that it could be expensive.

I'm in favor of this.

/cc @aclements @rsc

nesty92 added a commit to nesty92/go that referenced this issue Nov 12, 2024
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 golang#70280

Change-Id: I3eb3bc60658abc5de95697a10bddd11ab54c6e13
GitHub-Last-Rev: 5c37e88
GitHub-Pull-Request: golang#70281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

Successfully merging a pull request may close this issue.

6 participants