-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
zapslog.Handler: Add stack traces #1329
Comments
This is a valid request and probably a prerequisite for the slog handler before it's moved to the main module and considered stable. Currently, there's only a TODO in its place: Lines 139 to 140 in b454e18
RE: implementation: We wouldn't necessarily export the stacktrace formatter from the public Zap package. We would move it to an internal package and access it from there in both places. |
I indeed mentioned export the stack formatter if |
@abhinav i managed to make it work locally by exporting func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
zapLevel := convertSlogLevel(record.Level)
ent := zapcore.Entry{
Level: zapLevel,
Time: record.Time,
Message: record.Message,
LoggerName: h.name,
}
ce := h.core.Check(ent, nil)
if ce == nil {
return nil
}
if h.addSource && record.PC != 0 {
frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()
if frame.PC != 0 {
ce.Caller = zapcore.EntryCaller{
Defined: true,
PC: frame.PC,
File: frame.File,
Line: frame.Line,
Function: frame.Function,
}
}
}
if zapcore.LevelEnabler.Enabled(h.addStack, zapLevel) {
ce.Stack = zap.TakeStacktrace(2 + h.skipStack)
}
fields := make([]zapcore.Field, 0, record.NumAttrs())
record.Attrs(func(attr slog.Attr) bool {
fields = append(fields, convertAttrToField(attr))
return true
})
ce.Write(fields...)
return nil
} Handler options have been updated like so: type HandlerOptions struct {
// LoggerName is used for log entries received from slog.
//
// Defaults to empty.
LoggerName string
// AddSource configures the handler to annotate each message with the filename,
// line number, and function name.
// AddSource is false by default to skip the cost of computing
// this information.
AddSource bool
AddStack *zapcore.Level
SkipStack int
} However i realized we might want to use a similar functionnal options pattern like we have for the wdyt? |
@zekth That's great! When you feel it's ready, feel free to turn that into a PR. We originally went with an One caveat: we will not be able to re-use |
Ok, consider i'm writing the implementation of stacktrace with the functional options then. Should be ready within few days. |
fix: #1329 As discussed in the issue, replacing the `HandlersOptions` with functional options, i also renamed `addSource` to `addCaller` to properly match the `zap` semantic. --------- Co-authored-by: Abhinav Gupta <mail@abhinavg.net>
First i'm really happy to see the fast work on making zap compliant on go 1.21 with
slog
!Is your feature request related to a problem? Please describe.
This describes just the miss of
stack trace
serialization via the slog handler.Describe the solution you'd like
Taking this example:
It outputs:
I'd hope to find a solution to be able to have the
stack_trace
also via theslog
handler.Describe alternatives you've considered
Because we only pass the
zapcore.Core
to thezapslog
handler we have access toStacktraceKey
, however we don't have access to theAddStacktrace
options which are on thezap
logger itself. What we can do is like theAddSource
option inzapslog.HandlerOptions
we can also pass the arguments to enable the stacktrace on a certain level. That would mean then we need to export the stacktraceformatter and other required pieces to be able to serialize the stack trace: https://github.com/uber-go/zap/blob/b454e1885dd30fc65bf5d5b40827d1af2d2c57db/stacktrace.go#L147C10-L147C10Wdyt?
Is this a breaking change?
No
The text was updated successfully, but these errors were encountered: