-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
117 lines (92 loc) · 3.39 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
107
108
109
110
111
112
113
114
115
116
117
package logger
import (
"context"
"fmt"
"io"
"log/slog"
"os"
)
type (
Logger interface {
Debug(ctx context.Context, msg string, args ...any)
Info(ctx context.Context, msg string, args ...any)
Warn(ctx context.Context, msg string, args ...any)
Error(ctx context.Context, msg string, args ...any)
DebugWithFields(ctx context.Context, msg string, fields map[string]interface{})
InfoWithFields(ctx context.Context, msg string, fields map[string]interface{})
WarnWithFields(ctx context.Context, msg string, fields map[string]interface{})
ErrorWithFields(ctx context.Context, msg string, fields map[string]interface{})
Debugf(ctx context.Context, format string, args ...any)
Infof(ctx context.Context, format string, args ...any)
Warnf(ctx context.Context, format string, args ...any)
Errorf(ctx context.Context, format string, args ...any)
}
logger struct {
slog *slog.Logger
serviceName string
}
)
func NewLogger(serviceName string, level slog.Level, out io.Writer) Logger {
if out == nil {
out = os.Stdout
}
opts := &slog.HandlerOptions{
Level: level,
}
handler := slog.NewJSONHandler(out, opts).WithAttrs([]slog.Attr{
slog.String("service", serviceName),
})
slogLogger := slog.New(handler)
return &logger{slog: slogLogger, serviceName: serviceName}
}
func (l *logger) log(ctx context.Context, level slog.Level, msg string, args ...any) {
l.slog.LogAttrs(ctx, level, msg,
slog.Any("data", args))
}
func (l *logger) logWithFields(ctx context.Context, level slog.Level, msg string, fields map[string]interface{}) {
attrs := make([]slog.Attr, 0)
for k, v := range fields {
attrs = append(attrs, slog.Any(k, v))
}
l.slog.LogAttrs(ctx, level, msg, attrs...)
}
func (l *logger) logf(ctx context.Context, level slog.Level, format string, args ...any) {
msg := fmt.Sprintf(format, args...)
l.slog.LogAttrs(ctx, level, msg)
}
func (l *logger) Debug(ctx context.Context, msg string, args ...any) {
l.log(ctx, slog.LevelDebug, msg, args...)
}
func (l *logger) Info(ctx context.Context, msg string, args ...any) {
l.log(ctx, slog.LevelInfo, msg, args...)
}
func (l *logger) Warn(ctx context.Context, msg string, args ...any) {
l.log(ctx, slog.LevelWarn, msg, args...)
}
func (l *logger) Error(ctx context.Context, msg string, args ...any) {
l.log(ctx, slog.LevelError, msg, args...)
}
func (l *logger) DebugWithFields(ctx context.Context, msg string, fields map[string]interface{}) {
l.logWithFields(ctx, slog.LevelDebug, msg, fields)
}
func (l *logger) InfoWithFields(ctx context.Context, msg string, fields map[string]interface{}) {
l.logWithFields(ctx, slog.LevelInfo, msg, fields)
}
func (l *logger) WarnWithFields(ctx context.Context, msg string, fields map[string]interface{}) {
l.logWithFields(ctx, slog.LevelWarn, msg, fields)
}
func (l *logger) ErrorWithFields(ctx context.Context, msg string, fields map[string]interface{}) {
l.logWithFields(ctx, slog.LevelError, msg, fields)
}
func (l *logger) Debugf(ctx context.Context, format string, args ...any) {
l.logf(ctx, slog.LevelDebug, format, args...)
}
func (l *logger) Infof(ctx context.Context, format string, args ...any) {
l.logf(ctx, slog.LevelInfo, format, args...)
}
func (l *logger) Warnf(ctx context.Context, format string, args ...any) {
l.logf(ctx, slog.LevelWarn, format, args...)
}
func (l *logger) Errorf(ctx context.Context, format string, args ...any) {
l.logf(ctx, slog.LevelError, format, args...)
}