-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
106 lines (89 loc) · 2.77 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
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
package corelog
import (
"context"
"fmt"
"log/slog"
"runtime"
"time"
)
// Logger is a logger that wraps the slog package.
type Logger struct {
name string
handler slog.Handler
}
// NewLogger returns a new named logger.
func NewLogger(name string) *Logger {
return &Logger{
name: name,
handler: &namedHandler{name: name},
}
}
// WithAttrs returns a new Logger whose attributes consist of
// both the receiver's attributes and the arguments.
func (l *Logger) WithAttrs(attrs ...slog.Attr) *Logger {
return &Logger{
name: l.name,
handler: l.handler.WithAttrs(attrs),
}
}
// WithGroup returns a new Logger with the given group appended to
// the receiver's existing groups.
func (l *Logger) WithGroup(name string) *Logger {
return &Logger{
name: l.name,
handler: l.handler.WithGroup(name),
}
}
// Info logs a message at info log level.
func (l *Logger) Info(msg string, args ...slog.Attr) {
l.log(context.Background(), slog.LevelInfo, nil, msg, args)
}
// InfoContext logs a message at info log level.
func (l *Logger) InfoContext(ctx context.Context, msg string, args ...slog.Attr) {
l.log(ctx, slog.LevelInfo, nil, msg, args)
}
// Error logs a message at error log level.
func (l *Logger) Error(msg string, args ...slog.Attr) {
l.log(context.Background(), slog.LevelError, nil, msg, args)
}
// ErrorE logs a message at error log level with an error stacktrace.
func (l *Logger) ErrorE(msg string, err error, args ...slog.Attr) {
l.log(context.Background(), slog.LevelError, err, msg, args)
}
// ErrorContext logs a message at error log level.
func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...slog.Attr) {
l.log(ctx, slog.LevelError, nil, msg, args)
}
// ErrorContextE logs a message at error log level with an error stacktrace.
func (l *Logger) ErrorContextE(ctx context.Context, msg string, err error, args ...slog.Attr) {
l.log(ctx, slog.LevelError, err, msg, args)
}
// log wraps calls to the underlying logger so that the caller source can be corrected and
// an optional stacktrace can be included.
func (l *Logger) log(ctx context.Context, level slog.Level, err error, msg string, args []slog.Attr) {
// check if logger is enabled
if !l.handler.Enabled(ctx, level) {
return
}
// use latest config values
config := GetConfig(l.name)
var pcs [1]uintptr
// add caller source if enabled
if config.EnableSource {
runtime.Callers(3, pcs[:]) // skip [Callers, log, Info]
}
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
// add logger name
r.Add(nameKey, l.name)
// add remaining attributes
r.AddAttrs(args...)
// add stack trace if enabled
if err != nil && config.EnableStackTrace {
r.Add(stackKey, fmt.Sprintf("%+v", err))
}
// add error if not nil
if err != nil {
r.Add(errorKey, err.Error())
}
_ = l.handler.Handle(ctx, r)
}