-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
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 https://go.dev/cl/626976 mentions this issue: |
Do we need to export it when it's just a single call to runtime.CallersFrames? |
@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. |
CC @jba |
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 |
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
Go version
go version go1.23.1 linux/amd64
Output of
go env
in your module/workspace: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.
The text was updated successfully, but these errors were encountered: