-
Notifications
You must be signed in to change notification settings - Fork 0
/
slogx.go
69 lines (57 loc) · 1.98 KB
/
slogx.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
// Package slogx contains extensions for standard library's slog package.
package slogx
import (
"context"
"log/slog"
)
// LogAttrStrategy specifies what to do with the attribute that
// is about to be logged.
type LogAttrStrategy uint8
const (
// LogAttrNone means that the attribute should not be logged.
LogAttrNone LogAttrStrategy = iota
// LogAttrAsIs means that the attribute should be logged as is.
LogAttrAsIs
)
// Common used keys.
var (
ErrorKey = "error"
RequestIDKey = "request_id"
)
// HandleFunc is a function that handles a record.
type HandleFunc func(context.Context, slog.Record) error
// Middleware is a middleware for logging handler.
type Middleware func(HandleFunc) HandleFunc
// ErrAttrStrategy specifies how to log errors.
// "AsIs" logs nils, when the error is nil, if you want to not
// log nils, use "None".
// Example:
//
// 2024/01/13 15:20:26 ERROR LogAttrAsIs, error | error="some error"
// 2024/01/13 15:20:26 ERROR LogAttrAsIs, nil | error=<nil>
// 2024/01/13 15:20:26 ERROR LogAttrNone, error | error="some error"
// 2024/01/13 15:20:26 ERROR LogAttrNone, nil |
var ErrAttrStrategy = LogAttrAsIs
// Error returns an attribute with error key.
func Error(err error) slog.Attr {
if err == nil && ErrAttrStrategy == LogAttrNone {
return slog.Attr{}
}
return slog.Any(ErrorKey, err)
}
// Attrs returns attributes from the given record.
func Attrs(rec slog.Record) []slog.Attr {
var attrs []slog.Attr
rec.Attrs(func(attr slog.Attr) bool {
attrs = append(attrs, attr)
return true
})
return attrs
}
// NopHandler returns a slog.Handler, that does nothing.
func NopHandler() slog.Handler { return nopHandler{} }
type nopHandler struct{}
func (nopHandler) Enabled(context.Context, slog.Level) bool { return false }
func (nopHandler) Handle(context.Context, slog.Record) error { return nil }
func (n nopHandler) WithAttrs([]slog.Attr) slog.Handler { return n }
func (n nopHandler) WithGroup(string) slog.Handler { return n }