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:
package main
import (
"errors"
"log/slog"
"os"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
)
func main() {
config := zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "ts",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
StacktraceKey: "stack_trace",
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(config),
zapcore.Lock(os.Stdout),
zap.DebugLevel,
)
zapInstance := zap.New(core,
zap.AddCaller(),
zap.AddStacktrace(zapcore.ErrorLevel),
)
slogInstance := slog.New(zapslog.NewHandler(zapInstance.Core(), &zapslog.HandlerOptions{AddSource: true}))
err := errors.New("failure")
zapInstance.Error("zap", zap.Error(err))
slogInstance.Error("slog", slog.Any("error", err))
}
It outputs:
{"level":"ERROR","ts":"2023-08-19T12:10:36.636+0200","caller":"go/main.go:35","msg":"zap","error":"failure","stack_trace":"main.main\n\t/Users/vincent/kong/go/main.go:35\nruntime.main\n\t/usr/local/Cellar/go/1.21.0/libexec/src/runtime/proc.go:267"}
{"level":"ERROR","ts":"2023-08-19T12:10:36.636+0200","caller":"go/main.go:36","msg":"slog","error":"failure"}
I'd hope to find a solution to be able to have the stack_trace also via the slog handler.
Describe alternatives you've considered
Because we only pass the zapcore.Core to the zapslog handler we have access to StacktraceKey, however we don't have access to the AddStacktrace options which are on the zap logger itself. What we can do is like the AddSource option in zapslog.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-L147C10
Wdyt?
Is this a breaking change?
No
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 traceserialization 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_tracealso via thesloghandler.Describe alternatives you've considered
Because we only pass the
zapcore.Coreto thezapsloghandler we have access toStacktraceKey, however we don't have access to theAddStacktraceoptions which are on thezaplogger itself. What we can do is like theAddSourceoption inzapslog.HandlerOptionswe 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