-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlogger.go
More file actions
44 lines (38 loc) · 1.04 KB
/
logger.go
File metadata and controls
44 lines (38 loc) · 1.04 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
package main
import (
"context"
"log/slog"
"runtime"
)
type CustLogger struct {
base *slog.Logger
}
func NewCustLoggerLogger(base *slog.Logger) *CustLogger {
return &CustLogger{base: base}
}
func getCallerInfo() (file string, line int, funcName string) {
pc, file, line, ok := runtime.Caller(2)
if !ok {
return "unknown", 0, "unknown"
}
funcName = runtime.FuncForPC(pc).Name()
return file, line, funcName
}
func (l *CustLogger) InfoContext(ctx context.Context, msg string, keysAndValues ...any) {
file, line, funcName := getCallerInfo()
extendedKeysAndValues := append(keysAndValues,
"caller.file", file,
"caller.line", line,
"caller.func", funcName,
)
l.base.InfoContext(ctx, msg, extendedKeysAndValues...)
}
func (l *CustLogger) ErrorContext(ctx context.Context, err error, keysAndValues ...any) {
file, line, funcName := getCallerInfo()
extendedKeysAndValues := append(keysAndValues,
"caller.file", file,
"caller.line", line,
"caller.func", funcName,
)
l.base.ErrorContext(ctx, err.Error(), extendedKeysAndValues...)
}