-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
59 lines (54 loc) · 1.25 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
58
59
package logger
import (
"io"
"log/slog"
"os"
"strings"
)
const (
Debug = slog.Level(-4)
Info = slog.Level(0)
Warn = slog.Level(4)
Error = slog.Level(8)
Fatal = slog.Level(12)
)
// NewLogger takes a writer and an optional ENV value. It returns a new logger.
// The default writer is os.Stdout if nil and the logging level is ERROR if not defined.
// Logging levels: DEBUG = -4, INFO = 0, WARN = 4, ERROR = 8, FATAL = 12.
func NewLogger(writer io.Writer, level ...string) (logger *slog.Logger) {
logLevel := Error
if len(level) > 0 {
setLevel := strings.ToUpper(level[0])
switch setLevel {
case "DEBUG":
logLevel = Debug
case "INFO":
logLevel = Info
case "WARN":
logLevel = Warn
case "ERROR":
logLevel = Error
case "FATAL":
logLevel = Fatal
}
}
if writer == nil {
writer = os.Stdout
}
opts := &slog.HandlerOptions{
Level: logLevel,
// Replaces "ERROR+4" with "FATAL" in log output
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
levelLabel := level.String()
if level == Fatal {
levelLabel = "FATAL"
}
a.Value = slog.StringValue(levelLabel)
}
return a
},
}
return slog.New(slog.NewTextHandler(writer, opts))
}