-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
176 lines (130 loc) · 4.15 KB
/
logger.go
File metadata and controls
176 lines (130 loc) · 4.15 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package logger
import (
"context"
"fmt"
"log/slog"
"os"
)
//nolint:gochecknoglobals // ...
var globalLogger *slog.Logger = slog.New(newLoggerHandler(LevelDebug, os.Stdout))
func SetGlobalLogger(l *slog.Logger) {
globalLogger = l
}
func SetLevel(l slog.Level) {
globalLogger = slog.New(newLoggerHandler(l, os.Stdout))
}
const (
LevelEmergency = slog.Level(10000)
LevelAlert = slog.Level(1000)
LevelCritial = slog.Level(100)
LevelError = slog.LevelError
LevelWarn = slog.LevelWarn
LevelNotice = slog.Level(2)
LevelInfo = slog.LevelInfo
LevelDebug = slog.LevelDebug
)
type LogFunc func(context.Context, string, ...any)
var (
Falalf LogFunc = FatalKV
Emergencyf LogFunc = EmergencyKV
Alertf LogFunc = AlertKV
Critialf LogFunc = CritialKV
Errorf LogFunc = ErrorKV
Warnf LogFunc = WarnKV
Noticef LogFunc = NoticeKV
Infof LogFunc = InfoKV
Debugf LogFunc = DebugKV
)
func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context {
l := loggerFromCtx(ctx)
if l == globalLogger {
lcopy := *l
l = &lcopy
}
for _, a := range attrs {
l = l.With(a)
}
return context.WithValue(ctx, loggerKey, l)
}
func WithGroup(ctx context.Context, name string) context.Context {
l := loggerFromCtx(ctx)
if l == globalLogger {
lcopy := *l
l = &lcopy
}
return context.WithValue(ctx, loggerKey, l.WithGroup(name))
}
func Fatal(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, message, attrs...)
panic(fmt.Sprintf(message, attrs...))
}
func Emergency(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, message, attrs...)
}
func Alert(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelAlert, message, attrs...)
}
func Critial(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelCritial, message, attrs...)
}
func Error(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.ErrorContext(ctx, message, attrs...)
}
func Warn(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.WarnContext(ctx, message, attrs...)
}
func Notice(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelNotice, message, attrs...)
}
func Info(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.InfoContext(ctx, message, attrs...)
}
func Debug(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.DebugContext(ctx, message, attrs...)
}
func FatalKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, fmt.Sprintf(message, attrs...))
panic(fmt.Sprintf(message, attrs...))
}
func EmergencyKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelEmergency, fmt.Sprintf(message, attrs...))
}
func AlertKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelAlert, fmt.Sprintf(message, attrs...))
}
func CritialKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelCritial, fmt.Sprintf(message, attrs...))
}
func ErrorKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.ErrorContext(ctx, fmt.Sprintf(message, attrs...))
}
func WarnKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.WarnContext(ctx, fmt.Sprintf(message, attrs...))
}
func NoticeKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.Log(ctx, LevelNotice, fmt.Sprintf(message, attrs...))
}
func InfoKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.InfoContext(ctx, fmt.Sprintf(message, attrs...))
}
func DebugKV(ctx context.Context, message string, attrs ...any) {
l := loggerFromCtx(ctx)
l.DebugContext(ctx, fmt.Sprintf(message, attrs...))
}