logutil.go 1.11 KB
Newer Older
1
2
3
package logutil

import (
4
	"context"
5
6
7
	"io"
	"log/slog"
	"path/filepath"
8
9
	"runtime"
	"time"
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
)

const LevelTrace slog.Level = -8

func NewLogger(w io.Writer, level slog.Level) *slog.Logger {
	return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{
		Level:     level,
		AddSource: true,
		ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
			switch attr.Key {
			case slog.LevelKey:
				switch attr.Value.Any().(slog.Level) {
				case LevelTrace:
					attr.Value = slog.StringValue("TRACE")
				}
			case slog.SourceKey:
				source := attr.Value.Any().(*slog.Source)
				source.File = filepath.Base(source.File)
			}
			return attr
		},
	}))
}
33

34
35
type key string

36
func Trace(msg string, args ...any) {
37
	TraceContext(context.WithValue(context.TODO(), key("skip"), 1), msg, args...)
38
39
40
}

func TraceContext(ctx context.Context, msg string, args ...any) {
41
42
43
44
45
46
47
	if logger := slog.Default(); logger.Enabled(ctx, LevelTrace) {
		skip, _ := ctx.Value(key("skip")).(int)
		pc, _, _, _ := runtime.Caller(1 + skip)
		record := slog.NewRecord(time.Now(), LevelTrace, msg, pc)
		record.Add(args...)
		logger.Handler().Handle(ctx, record)
	}
48
}