-
Notifications
You must be signed in to change notification settings - Fork 14
/
logger.go
52 lines (42 loc) · 984 Bytes
/
logger.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
package slacker
import (
"log/slog"
"os"
)
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
type builtinLogger struct {
debugMode bool
logger *slog.Logger
}
func newBuiltinLogger(debugMode bool) *builtinLogger {
opts := &slog.HandlerOptions{
Level: getLogLevel(debugMode),
}
return &builtinLogger{
debugMode: debugMode,
logger: slog.New(slog.NewJSONHandler(os.Stdout, opts)),
}
}
func (l *builtinLogger) Info(msg string, args ...any) {
l.logger.Info(msg, args...)
}
func (l *builtinLogger) Debug(msg string, args ...any) {
l.logger.Debug(msg, args...)
}
func (l *builtinLogger) Warn(msg string, args ...any) {
l.logger.Warn(msg, args...)
}
func (l *builtinLogger) Error(msg string, args ...any) {
l.logger.Error(msg, args...)
}
func getLogLevel(isDebugMode bool) slog.Level {
if isDebugMode {
return slog.LevelDebug
}
return slog.LevelInfo
}