/
model.go
55 lines (45 loc) · 1.04 KB
/
model.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
package logger
import (
"context"
"time"
"log/slog"
)
// Level represents different logging levels.
type Level slog.Level
// A set of possible logging levels.
const (
LevelDebug = Level(slog.LevelDebug)
LevelInfo = Level(slog.LevelInfo)
LevelWarn = Level(slog.LevelWarn)
LevelError = Level(slog.LevelError)
)
// Record represents the data that is being logged.
type Record struct {
Time time.Time
Message string
Level Level
Attributes map[string]any
}
func toRecord(r slog.Record) Record {
atts := make(map[string]any, r.NumAttrs())
f := func(attr slog.Attr) bool {
atts[attr.Key] = attr.Value.Any()
return true
}
r.Attrs(f)
return Record{
Time: r.Time,
Message: r.Message,
Level: Level(r.Level),
Attributes: atts,
}
}
// EventFn is a function to be executed when configured against a log level.
type EventFn func(ctx context.Context, r Record)
// Events contains an assignment of an event function to a log level.
type Events struct {
Debug EventFn
Info EventFn
Warn EventFn
Error EventFn
}