-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
92 lines (75 loc) · 2.17 KB
/
example.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Code generated by go-simpler.org/sloggen. DO NOT EDIT.
package example
import (
"context"
"fmt"
"log/slog"
"runtime"
"strings"
"time"
)
const LevelInfo = slog.Level(0)
const LevelAlert = slog.Level(12)
const RequestId = "request_id"
func CreatedAt(value time.Time) slog.Attr { return slog.Time("created_at", value) }
func Err(value error) slog.Attr { return slog.Any("err", value) }
func UserId(value int) slog.Attr { return slog.Int("user_id", value) }
func ParseLevel(s string) (slog.Level, error) {
switch strings.ToUpper(s) {
case "INFO":
return LevelInfo, nil
case "ALERT":
return LevelAlert, nil
default:
return 0, fmt.Errorf("slog: level string %q: unknown name", s)
}
}
func RenameLevels(_ []string, attr slog.Attr) slog.Attr {
if attr.Key != slog.LevelKey {
return attr
}
switch attr.Value.Any().(slog.Level) {
case LevelInfo:
attr.Value = slog.StringValue("INFO")
case LevelAlert:
attr.Value = slog.StringValue("ALERT")
}
return attr
}
type Logger struct{ handler slog.Handler }
func New(h slog.Handler) *Logger { return &Logger{handler: h} }
func (l *Logger) Handler() slog.Handler { return l.handler }
func (l *Logger) Enabled(ctx context.Context, level slog.Level) bool {
return l.handler.Enabled(ctx, level)
}
func (l *Logger) With(attrs ...slog.Attr) *Logger {
if len(attrs) == 0 {
return l
}
return &Logger{handler: l.handler.WithAttrs(attrs)}
}
func (l *Logger) WithGroup(name string) *Logger {
if name == "" {
return l
}
return &Logger{handler: l.handler.WithGroup(name)}
}
func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
l.log(ctx, level, msg, attrs)
}
func (l *Logger) Info(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, LevelInfo, msg, attrs)
}
func (l *Logger) Alert(ctx context.Context, msg string, attrs ...slog.Attr) {
l.log(ctx, LevelAlert, msg, attrs)
}
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, attrs []slog.Attr) {
if !l.handler.Enabled(ctx, level) {
return
}
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.AddAttrs(attrs...)
_ = l.handler.Handle(ctx, r)
}