-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: log/slog: export Source method in Record to support custom handler implementations #70280
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
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
Can someone write up the proposed API? Thanks. |
The proposal is to export the existing
|
This proposal has been added to the active column of the proposals project |
Just for reference, the unexported implementation of |
I think it's unclear from the documentation whether Source would scan the Record's attributes, since we do have special handling for *Source-typed attributes in the built-in handlers. Presumably the answer is that it would not, and exclusively symbolizes Record.PC, but it would be good to clarify that for public documentation. |
If we're going to always return a non-nil *Source, we should say something about whether the result aliases with other calls to Source. Or maybe this should just return a nil source in that case? The documentation could also be a bit clearer that the result is always non-nil. You can get that from the proposed documentation, but it takes a little thought. |
How about this:
Here, "new" is supposed to imply that it doesn't alias. |
isn't it nicer to write: if s := r.Source(); s != nil {
// encode s
} vs if s := r.Source(), *s != (slog.Source{}) {
// encode s
} if r.PC != 0 {
s := r.Source()
// encode s
} |
That's a good point. I don't see a good reason to return a non-nil Source. I think that's left over from when we were using a value rather than a pointer.
|
Based on the discussion above, this proposal seems like a likely accept. The proposal is to add the following to package // Source returns a new Source for the log event using r's PC.
// If the PC field is zero, meaning the Record was created without the necessary information
// or the location is unavailable, then nil is returned.
func (r Record) Source() *Source (This is almost though not quite the existing unexported |
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 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
@aclements @jba I updated https://go-review.googlesource.com/c/go/+/626976 following the proposal decision |
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
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
Following proposal golang#70280 Signed-off-by: Ernesto Alejandro Santana Hidalgo <ernesto.alejandrosantana@gmail.com>
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: