-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
57 lines (45 loc) · 1.16 KB
/
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
53
54
55
56
57
package main
import (
"log"
"log/slog"
"os"
)
var logger = NewSLogLogger()
type SLogLogger struct {
log *slog.Logger // slog Logger instance
}
var logLevel = new(slog.LevelVar) // Log Level control variable
func NewSLogLogger() *SLogLogger {
sl := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
return &SLogLogger{log: sl}
}
func (sl *SLogLogger) Debug(msg string, metadata ...interface{}) {
sl.log.Debug(msg, metadata...)
}
func (sl *SLogLogger) Info(msg string, metadata ...interface{}) {
sl.log.Info(msg, metadata...)
}
func (sl *SLogLogger) Warn(msg string, metadata ...interface{}) {
sl.log.Warn(msg, metadata...)
}
func (sl *SLogLogger) Error(msg string, metadata ...interface{}) {
sl.log.Error(msg, metadata...)
}
func (sl *SLogLogger) Fatal(msg string, metadata ...interface{}) {
sl.log.Error(msg, metadata...)
log.Fatal(msg)
}
func (sl *SLogLogger) SetLevel(level string) {
switch level {
case "DEBUG":
logLevel.Set(slog.LevelDebug)
case "INFO":
logLevel.Set(slog.LevelInfo)
case "WARN":
logLevel.Set(slog.LevelWarn)
case "ERROR":
logLevel.Set(slog.LevelError)
default:
logLevel.Set(slog.LevelInfo)
}
}