-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathlogger_sub.go
More file actions
157 lines (126 loc) · 5.31 KB
/
logger_sub.go
File metadata and controls
157 lines (126 loc) · 5.31 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
package slog
import "context"
// SubLogger is a sub-logger, It can be used to keep a certain amount of contextual information and log multiple times.
// 可以用于保持一定的上下文信息多次记录日志。例如在循环中使用,或者作为方法参数传入。
//
// Usage:
//
// sl := slog.NewSub().KeepCtx(custom ctx).
// KeepFields(slog.M{"ip": ...}).
// KeepData(slog.M{"username": ...})
// defer sl.Release()
//
// sl.Info("some message")
type SubLogger struct {
l *Logger // parent logger
// Ctx keep context for all log records
Ctx context.Context
// Fields keep custom fields data for all log records
Fields M
// Data keep data for all log records
Data M
// Extra data. will keep for all log records
Extra M
}
// NewSubWith returns a new SubLogger with parent logger.
func NewSubWith(l *Logger) *SubLogger { return &SubLogger{l: l} }
// KeepCtx keep context for all log records
func (sub *SubLogger) KeepCtx(ctx context.Context) *SubLogger {
sub.Ctx = ctx
return sub
}
// KeepFields keep custom fields data for all log records
func (sub *SubLogger) KeepFields(fields M) *SubLogger {
sub.Fields = fields
return sub
}
// KeepField keep custom field for all log records
func (sub *SubLogger) KeepField(field string, value any) *SubLogger {
if sub.Fields == nil {
sub.Fields = make(M)
}
sub.Fields[field] = value
return sub
}
// KeepData keep data for all log records
func (sub *SubLogger) KeepData(data M) *SubLogger {
sub.Data = data
return sub
}
// KeepExtra keep extra data for all log records
func (sub *SubLogger) KeepExtra(extra M) *SubLogger {
sub.Extra = extra
return sub
}
// Release releases the SubLogger.
func (sub *SubLogger) Release() {
sub.l = nil
sub.Ctx = nil
sub.Fields = nil
sub.Data = nil
sub.Extra = nil
}
func (sub *SubLogger) withKeepCtx() *Record {
r := sub.l.WithContext(sub.Ctx)
r.Data = sub.Data
r.Extra = sub.Extra
r.Fields = sub.Fields
return r
}
//
// ---------------------------------------------------------------------------
// Add log message with level
// ---------------------------------------------------------------------------
//
// Print logs a message at PrintLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Print(args ...any) { sub.withKeepCtx().Print(args...) }
// Printf logs a message at PrintLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Printf(format string, args ...any) { sub.withKeepCtx().Printf(format, args...) }
// Trace logs a message at TraceLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Trace(args ...any) { sub.withKeepCtx().Trace(args...) }
// Tracef logs a formatted message at TraceLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Tracef(format string, args ...any) {
sub.withKeepCtx().Tracef(format, args...)
}
// Debug logs a message at DebugLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Debug(args ...any) { sub.withKeepCtx().Debug(args...) }
// Debugf logs a formatted message at DebugLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Debugf(format string, args ...any) {
sub.withKeepCtx().Debugf(format, args...)
}
// Info logs a message at InfoLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Info(args ...any) { sub.withKeepCtx().Info(args...) }
// Infof logs a formatted message at InfoLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Infof(format string, args ...any) {
sub.withKeepCtx().Infof(format, args...)
}
// Notice logs a message at NoticeLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Notice(args ...any) { sub.withKeepCtx().Notice(args...) }
// Noticef logs a formatted message at NoticeLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Noticef(format string, args ...any) {
sub.withKeepCtx().Noticef(format, args...)
}
// Warn logs a message at WarnLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Warn(args ...any) { sub.withKeepCtx().Warn(args...) }
// Warnf logs a formatted message at WarnLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Warnf(format string, args ...any) {
sub.withKeepCtx().Warnf(format, args...)
}
// Error logs a message at ErrorLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Error(args ...any) { sub.withKeepCtx().Error(args...) }
// Errorf logs a formatted message at ErrorLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Errorf(format string, args ...any) {
sub.withKeepCtx().Errorf(format, args...)
}
// Fatal logs a message at FatalLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Fatal(args ...any) { sub.withKeepCtx().Fatal(args...) }
// Fatalf logs a formatted message at FatalLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Fatalf(format string, args ...any) {
sub.withKeepCtx().Fatalf(format, args...)
}
// Panic logs a message at PanicLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Panic(args ...any) { sub.withKeepCtx().Panic(args...) }
// Panicf logs a formatted message at PanicLevel. will with sub logger's context, fields and data
func (sub *SubLogger) Panicf(format string, args ...any) {
sub.withKeepCtx().Panicf(format, args...)
}