Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
always log file line on terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 committed Nov 15, 2023
1 parent 8cf0589 commit 4c325dd
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions logger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ func newBaseLogger() *Logger {
}

func newTerminalLogger() *Logger {
src := Level.Level() == slog.LevelDebug
h := &callDepthHandler{
src: src,
sh: tint.NewHandler(os.Stderr, &tint.Options{
AddSource: src,
Level: Level,
})}
// skip Callers, this function, 2 slog pkg calls, 2 this pkg calls
h := withCallDepth(6, tint.NewHandler(os.Stderr, &tint.Options{
AddSource: true,
Level: Level,
}))

return &Logger{sl: slog.New(h)}
}
Expand All @@ -50,29 +48,32 @@ func newLogger() *Logger {
return &Logger{sl: slog.New(h)}
}

func withCallDepth(depth int, sh slog.Handler) slog.Handler {
return &callDepthHandler{depth: depth, sh: sh}
}

type callDepthHandler struct {
src bool
sh slog.Handler
depth int
sh slog.Handler
}

func (h *callDepthHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.sh.Enabled(ctx, level)
}

func (h *callDepthHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &callDepthHandler{src: h.src, sh: h.sh.WithAttrs(attrs)}
return &callDepthHandler{depth: h.depth, sh: h.sh.WithAttrs(attrs)}
}

func (h *callDepthHandler) WithGroup(name string) slog.Handler {
return &callDepthHandler{src: h.src, sh: h.sh.WithGroup(name)}
return &callDepthHandler{depth: h.depth, sh: h.sh.WithGroup(name)}
}

func (h *callDepthHandler) Handle(ctx context.Context, r slog.Record) error {
if h.src {
// https://pkg.go.dev/log/slog#example-package-Wrapping
var pcs [1]uintptr
runtime.Callers(6, pcs[:]) // skip Callers, this function, 2 slog pkg calls, 2 this pkg calls
r.PC = pcs[0]
}
// https://pkg.go.dev/log/slog#example-package-Wrapping
var pcs [1]uintptr
runtime.Callers(h.depth, pcs[:])
r.PC = pcs[0]

return h.sh.Handle(ctx, r)
}

0 comments on commit 4c325dd

Please sign in to comment.