-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip.go
46 lines (43 loc) · 1.25 KB
/
skip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package slogx
import (
"context"
"log/slog"
"runtime"
"time"
)
// LogSkip emits a log record using handler with the
// current time and the given level and message.
// Value skip=0 works exactly like (*slog.Logger).Log,
// value skip=1 skips caller of LogSkip() etc.
func LogSkip(ctx context.Context, skip int, handler slog.Handler, level slog.Level, msg string, args ...any) {
if ctx == nil {
ctx = context.Background()
}
if !handler.Enabled(ctx, level) {
return
}
const offset = 2
var pcs [1]uintptr
runtime.Callers(offset+skip, pcs[:])
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.Add(args...)
_ = handler.Handle(ctx, r)
}
// LogAttrsSkip emits a log record using handler with the
// current time and the given level and message.
// Value skip=0 works exactly like (*slog.Logger).LogAttrs,
// value skip=1 skips caller of LogAttrsSkip() etc.
func LogAttrsSkip(ctx context.Context, skip int, handler slog.Handler, level slog.Level, msg string, attrs ...slog.Attr) {
if ctx == nil {
ctx = context.Background()
}
if !handler.Enabled(ctx, level) {
return
}
const offset = 2
var pcs [1]uintptr
runtime.Callers(offset+skip, pcs[:])
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.AddAttrs(attrs...)
_ = handler.Handle(ctx, r)
}