Skip to content

Commit 1c3ba4a

Browse files
flimzythaJeztah
andcommitted
Optimizations and improvements from PR feedback
Co-authored-by: Jonathan Hall <jonathan@jhall.io> Co-authored-by: Sebastiaan van Stijn <github@gone.nl> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent f4f1cf0 commit 1c3ba4a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

hooks/slog/slog.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package slog
55
import (
66
"context"
77
"log/slog"
8-
"runtime"
98
"slices"
109

1110
"github.com/sirupsen/logrus"
@@ -81,6 +80,10 @@ func (h *SlogHook) Fire(entry *logrus.Entry) error {
8180
ctx = context.Background()
8281
}
8382
lvl := h.toSlogLevel(entry.Level).Level()
83+
handler := h.logger.Handler()
84+
if !handler.Enabled(ctx, lvl) {
85+
return nil
86+
}
8487
keys := make([]string, 0, len(entry.Data))
8588
for k := range entry.Data {
8689
keys = append(keys, k)
@@ -90,10 +93,11 @@ func (h *SlogHook) Fire(entry *logrus.Entry) error {
9093
for _, k := range keys {
9194
attrs = append(attrs, slog.Any(k, entry.Data[k]))
9295
}
93-
var pcs [1]uintptr
94-
// skip 8 callers to get to the original logrus caller
95-
runtime.Callers(8, pcs[:])
96-
r := slog.NewRecord(entry.Time, lvl, entry.Message, pcs[0])
96+
var pc uintptr
97+
if entry.Caller != nil {
98+
pc = entry.Caller.PC
99+
}
100+
r := slog.NewRecord(entry.Time, lvl, entry.Message, pc)
97101
r.AddAttrs(attrs...)
98-
return h.logger.Handler().Handle(ctx, r)
102+
return handler.Handle(ctx, r)
99103
}

0 commit comments

Comments
 (0)