-
Notifications
You must be signed in to change notification settings - Fork 11
/
logging.go
70 lines (62 loc) · 1.09 KB
/
logging.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
package provider
import (
"encoding/json"
"fmt"
"log/slog"
"strings"
)
type Level string
const (
Error Level = "error"
Warn Level = "warn"
Info Level = "info"
Debug Level = "debug"
Trace Level = "trace"
Critical Level = "critical"
)
func (l Level) String() string {
return string(l)
}
func (l Level) Level() slog.Level {
switch l {
case Error:
return slog.LevelError
case Warn:
return slog.LevelWarn
case Info:
return slog.LevelInfo
case Debug:
return slog.LevelDebug
// NOTE: slog doesn't have trace/critical levels so we map them to debug/error
case Trace:
return slog.LevelDebug
case Critical:
return slog.LevelError
default:
return slog.LevelInfo
}
}
func (l *Level) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
switch strings.ToLower(s) {
case "error":
*l = Error
case "warn":
*l = Warn
case "info":
*l = Info
case "debug":
*l = Debug
case "trace":
*l = Trace
case "critical":
*l = Critical
default:
return fmt.Errorf("invalid level: %s", s)
}
return nil
}