-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
101 lines (81 loc) · 2.02 KB
/
log.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package log
import (
"context"
"log/slog"
"os"
"strconv"
"strings"
"sync/atomic"
)
var (
ReleaseMode bool
defaultLogger atomic.Pointer[slog.Logger]
logLoggerLevel slog.LevelVar
)
func init() {
ReleaseMode, _ = strconv.ParseBool(os.Getenv("LOG_PURE"))
opts := &slog.HandlerOptions{
Level: &logLoggerLevel,
}
level, found := os.LookupEnv("LOG_LEVEL")
if !found {
SetLogLoggerLevel(slog.LevelInfo)
goto INIT
}
switch strings.ToLower(level) {
case "debug":
SetLogLoggerLevel(slog.LevelDebug)
case "info":
SetLogLoggerLevel(slog.LevelInfo)
case "warn":
SetLogLoggerLevel(slog.LevelWarn)
case "error":
SetLogLoggerLevel(slog.LevelError)
}
INIT:
defaultLogger.Store(slog.New(NewHandler(opts, os.Stderr)))
}
func SetLogLoggerLevel(level slog.Level) (oldLevel slog.Level) {
oldLevel = logLoggerLevel.Level()
logLoggerLevel.Set(level)
return
}
func Default() *slog.Logger {
return defaultLogger.Load()
}
func SetDefault(l *slog.Logger) {
defaultLogger.Store(l)
}
func Debug(msg string, args ...any) {
Default().Debug(msg, args...)
}
func DebugContext(ctx context.Context, msg string, args ...any) {
Default().DebugContext(ctx, msg, args...)
}
func Info(msg string, args ...any) {
Default().Info(msg, args...)
}
func InfoContext(ctx context.Context, msg string, args ...any) {
Default().InfoContext(ctx, msg, args...)
}
func Warn(msg string, args ...any) {
Default().Warn(msg, args...)
}
func WarnContext(ctx context.Context, msg string, args ...any) {
Default().WarnContext(ctx, msg, args...)
}
func Error(msg string, args ...any) {
Default().Error(msg, args...)
}
func ErrorContext(ctx context.Context, msg string, args ...any) {
Default().ErrorContext(ctx, msg, args...)
}
func Log(ctx context.Context, level slog.Level, msg string, args ...any) {
Default().Log(ctx, level, msg, args...)
}
func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
Default().LogAttrs(ctx, level, msg, attrs...)
}
func With(args ...any) *slog.Logger {
return Default().With(args...)
}