summaryrefslogtreecommitdiff
path: root/internal/logging/trace.go
blob: 89007276ec48724939522770fd1d1a0faddf03fc (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package logging

import (
	"context"
	"log/slog"
	"runtime"
	"strconv"
	"strings"
)

const (
	prgCount = 20
	defSkip  = 6
)

type stackTracer struct {
	h     slog.Handler
	nSkip int
}

func (h stackTracer) Enabled(ctx context.Context, lvl slog.Level) bool {
	return h.h.Enabled(ctx, lvl)
}

func (h stackTracer) WithAttrs(attrs []slog.Attr) slog.Handler {
	return h.h.WithAttrs(attrs)
}

func (h stackTracer) WithGroup(name string) slog.Handler {
	return h.h.WithGroup(name)
}

func (h stackTracer) Handle(ctx context.Context, r slog.Record) error {
	if r.Level < slog.LevelError {
		return h.h.Handle(ctx, r)
	}

	trace := h.GetTrace()
	r.AddAttrs(slog.String("trace", trace))

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

func (h stackTracer) GetTrace() string {
	var b strings.Builder
	pc := make([]uintptr, prgCount)
	n := runtime.Callers(h.nSkip, pc)
	frames := runtime.CallersFrames(pc[:n])

	for frame, more := frames.Next(); more; frame, more = frames.Next() {
		b.WriteString(frame.Function + "\n    " + frame.File + ":" + strconv.Itoa(frame.Line) + "\n")
	}
	return b.String()
}

func withStackTrace(h slog.Handler) slog.Handler {
	return stackTracer{
		h:     h,
		nSkip: defSkip,
	}
}