-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
40 lines (32 loc) · 894 Bytes
/
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
package main
import "log/slog"
// structuredError is an error that can be logged with additional data
type structuredError struct {
err error
args []any
}
// NewStructuredError returns a new structure error that will be logged with additional data
func serror(err error, args ...any) *structuredError {
return &structuredError{err: err, args: args}
}
func (e *structuredError) Error() string {
return e.err.Error()
}
func (e *structuredError) Unwrap() error {
return e.err
}
// LogValue returns a structured log value for use with slog
func (e *structuredError) LogValue() slog.Value {
args := make([]slog.Attr, 0, (len(e.args)/2)+1)
args = append(args, slog.String(slog.MessageKey, e.Error()))
var c interface{}
for _, a := range e.args {
if c == nil {
c = a
continue
}
args = append(args, slog.Any(c.(string), a))
c = nil
}
return slog.GroupValue(args...)
}