-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger.go
294 lines (238 loc) · 6.73 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2024 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logit
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"runtime"
"time"
"github.com/FishGoddess/logit/defaults"
)
const (
keyBad = "!BADKEY"
keyPID = "pid"
)
var (
pid = os.Getpid()
)
// Syncer is an interface that syncs data to somewhere.
type Syncer interface {
Sync() error
}
// Logger is the entry of logging in logit.
// It has several levels including debug, info, warn and error.
// It's also a syncer or closer if handler is a syncer or closer.
type Logger struct {
handler slog.Handler
syncer Syncer
closer io.Closer
withSource bool
withPID bool
}
// NewLogger creates a logger with given options or panics if failed.
// If you don't want to panic on failing, use NewLoggerGracefully instead.
func NewLogger(opts ...Option) *Logger {
logger, err := NewLoggerGracefully(opts...)
if err != nil {
panic(err)
}
return logger
}
// NewLoggerGracefully creates a logger with given options or returns an error if failed.
// It's a more graceful way to create a logger than NewLogger function.
func NewLoggerGracefully(opts ...Option) (*Logger, error) {
conf := newDefaultConfig()
for _, opt := range opts {
opt.applyTo(conf)
}
handler, syncer, closer, err := conf.newHandler()
if err != nil {
return nil, err
}
logger := &Logger{
handler: handler,
syncer: syncer,
closer: closer,
withSource: conf.withSource,
withPID: conf.withPID,
}
if conf.syncTimer > 0 {
go logger.runSyncTimer(conf.syncTimer)
}
return logger, nil
}
func (l *Logger) runSyncTimer(d time.Duration) {
timer := time.NewTimer(d)
defer timer.Stop()
for {
select {
case <-timer.C:
if err := l.Sync(); err != nil {
defaults.HandleError("Logger.Sync", err)
}
}
}
}
func (l *Logger) clone() *Logger {
newLogger := *l
return &newLogger
}
func (l *Logger) squeezeAttr(args []any) (slog.Attr, []any) {
// len of args must be > 0
switch arg := args[0].(type) {
case slog.Attr:
return arg, args[1:]
case string:
if len(args) <= 1 {
return slog.String(keyBad, arg), nil
}
return slog.Any(arg, args[1]), args[2:]
default:
return slog.Any(keyBad, arg), args[1:]
}
}
func (l *Logger) newAttrs(args []any) (attrs []slog.Attr) {
var attr slog.Attr
for len(args) > 0 {
attr, args = l.squeezeAttr(args)
attrs = append(attrs, attr)
}
return attrs
}
// With returns a new logger with args.
// All logs from the new logger will carry the given args.
// See slog.Handler.WithAttrs.
func (l *Logger) With(args ...any) *Logger {
if len(args) <= 0 {
return l
}
attrs := l.newAttrs(args)
if len(attrs) <= 0 {
return l
}
newLogger := l.clone()
newLogger.handler = l.handler.WithAttrs(attrs)
return newLogger
}
// WithGroup returns a new logger with group name.
// All logs from the new logger will be grouped by the name.
// See slog.Handler.WithGroup.
func (l *Logger) WithGroup(name string) *Logger {
if name == "" {
return l
}
newLogger := l.clone()
newLogger.handler = l.handler.WithGroup(name)
return newLogger
}
// enabled reports whether the logger should ignore logs whose level is lower.
func (l *Logger) enabled(level slog.Level) bool {
return l.handler.Enabled(context.Background(), level)
}
// DebugEnabled reports whether the logger should ignore logs whose level is lower than debug.
func (l *Logger) DebugEnabled() bool {
return l.enabled(slog.LevelDebug)
}
// InfoEnabled reports whether the logger should ignore logs whose level is lower than info.
func (l *Logger) InfoEnabled() bool {
return l.enabled(slog.LevelInfo)
}
// WarnEnabled reports whether the logger should ignore logs whose level is lower than warn.
func (l *Logger) WarnEnabled() bool {
return l.enabled(slog.LevelWarn)
}
// ErrorEnabled reports whether the logger should ignore logs whose level is lower than error.
func (l *Logger) ErrorEnabled() bool {
return l.enabled(slog.LevelError)
}
// PrintEnabled reports whether the logger should ignore logs whose level is lower than print.
func (l *Logger) PrintEnabled() bool {
return l.enabled(defaults.LevelPrint)
}
func (l *Logger) newRecord(level slog.Level, msg string, args []any) slog.Record {
var pc uintptr
if l.withSource {
var pcs [1]uintptr
runtime.Callers(defaults.CallerDepth, pcs[:])
pc = pcs[0]
}
now := defaults.CurrentTime()
record := slog.NewRecord(now, level, msg, pc)
if l.withPID {
record.AddAttrs(slog.Int(keyPID, pid))
}
var attr slog.Attr
for len(args) > 0 {
attr, args = l.squeezeAttr(args)
record.AddAttrs(attr)
}
return record
}
func (l *Logger) log(level slog.Level, msg string, args ...any) {
if !l.enabled(level) {
return
}
record := l.newRecord(level, msg, args)
if err := l.handler.Handle(context.Background(), record); err != nil {
defaults.HandleError("Logger.handler.Handle", err)
}
}
// Debug logs a log with msg and args in debug level.
func (l *Logger) Debug(msg string, args ...any) {
l.log(slog.LevelDebug, msg, args...)
}
// Info logs a log with msg and args in info level.
func (l *Logger) Info(msg string, args ...any) {
l.log(slog.LevelInfo, msg, args...)
}
// Warn logs a log with msg and args in warn level.
func (l *Logger) Warn(msg string, args ...any) {
l.log(slog.LevelWarn, msg, args...)
}
// Error logs a log with msg and args in error level.
func (l *Logger) Error(msg string, args ...any) {
l.log(slog.LevelError, msg, args...)
}
// Printf logs a log with format and args in print level.
// It a old-school way to log.
func (l *Logger) Printf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
l.log(defaults.LevelPrint, msg)
}
// Print logs a log with args in print level.
// It a old-school way to log.
func (l *Logger) Print(args ...interface{}) {
msg := fmt.Sprint(args...)
l.log(defaults.LevelPrint, msg)
}
// Println logs a log with args in print level.
// It a old-school way to log.
func (l *Logger) Println(args ...interface{}) {
msg := fmt.Sprintln(args...)
l.log(defaults.LevelPrint, msg)
}
// Sync syncs the logger and returns an error if failed.
func (l *Logger) Sync() error {
return l.syncer.Sync()
}
// Close closes the logger and returns an error if failed.
func (l *Logger) Close() error {
if err := l.Sync(); err != nil {
return err
}
return l.closer.Close()
}