Skip to content

Commit

Permalink
fix: send the right source to zap -- also disable stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jan 21, 2024
1 parent 2c94794 commit 8f42baa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.A

// developer formatters
attrs = slogcommon.ReplaceError(attrs, ErrorKeys...)
if addSource {
attrs = append(attrs, slogcommon.Source(SourceKey, record))
}
// if addSource {
// attrs = append(attrs, slogcommon.Source(SourceKey, record))
// }
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, attrs...)

// handler formatter
Expand Down
18 changes: 17 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package slogzap

import (
"context"
"runtime"

"log/slog"

slogcommon "github.com/samber/slog-common"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type Option struct {
Expand Down Expand Up @@ -62,7 +64,21 @@ func (h *ZapHandler) Handle(ctx context.Context, record slog.Record) error {
level := LogLevels[record.Level]
fields := converter(h.option.AddSource, h.option.ReplaceAttr, h.attrs, h.groups, &record)

h.option.Logger.Log(level, record.Message, fields...)
checked := h.option.Logger.Check(level, record.Message)
if checked != nil {
if h.option.AddSource {
frame, _ := runtime.CallersFrames([]uintptr{record.PC}).Next()
checked.Caller = zapcore.NewEntryCaller(0, frame.File, frame.Line, true)
checked.Stack = "" //@TODO
} else {
checked.Caller = zapcore.EntryCaller{}
checked.Stack = ""
}
checked.Write(fields...)
return nil
} else {
h.option.Logger.Log(level, record.Message, fields...)
}

return nil
}
Expand Down

0 comments on commit 8f42baa

Please sign in to comment.