-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
45 lines (41 loc) · 932 Bytes
/
log.go
File metadata and controls
45 lines (41 loc) · 932 Bytes
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 plexus
import (
"log/slog"
"os"
"path/filepath"
"strings"
)
var LoggingLevel = new(slog.LevelVar)
func SetUpLogging(v string) {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
Level: LoggingLevel,
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.TimeKey {
return slog.Attr{}
}
if attr.Key == slog.SourceKey {
if s, _ := attr.Value.Any().(*slog.Source); s != nil {
s.File = filepath.Base(s.File)
}
}
return attr
},
})))
SetLogging(v)
}
func SetLogging(v string) {
switch strings.ToUpper(v) {
case "DEBUG":
LoggingLevel.Set(slog.LevelDebug)
case "INFO":
LoggingLevel.Set(slog.LevelInfo)
case "WARN":
LoggingLevel.Set(slog.LevelWarn)
case "ERROR":
LoggingLevel.Set(slog.LevelError)
default:
LoggingLevel.Set(slog.LevelInfo)
}
slog.Info("set log level", "level", LoggingLevel)
}