-
Notifications
You must be signed in to change notification settings - Fork 9
/
types.go
45 lines (35 loc) · 1.17 KB
/
types.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
package orchestra
import (
"context"
"log/slog"
)
// Logger is accepted by some Players ([Conductor], [ServerPlayer])
type Logger interface {
Info(msg string, attrs ...slog.Attr)
Error(msg string, attrs ...slog.Attr)
WithGroup(name string) Logger
}
var _ slogInterface = &slog.Logger{}
type slogInterface interface {
LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
WithGroup(name string) *slog.Logger
}
func LoggerFromSlog(infoLevel, errorLevel slog.Level, l slogInterface) Logger {
return slogLogger{infoLevel, errorLevel, l}
}
// DefaultLogger is used when a conductor's logger is nil
var DefaultLogger Logger = LoggerFromSlog(slog.LevelInfo, slog.LevelError, slog.Default())
type slogLogger struct {
lvlInfo slog.Level
lvlError slog.Level
logger slogInterface
}
func (d slogLogger) Info(msg string, attrs ...slog.Attr) {
d.logger.LogAttrs(context.Background(), d.lvlInfo, msg, attrs...)
}
func (d slogLogger) Error(msg string, attrs ...slog.Attr) {
d.logger.LogAttrs(context.Background(), d.lvlError, msg, attrs...)
}
func (d slogLogger) WithGroup(name string) Logger {
return slogLogger{d.lvlInfo, d.lvlError, d.logger.WithGroup(name)}
}