-
Notifications
You must be signed in to change notification settings - Fork 1
/
logadapter.go
138 lines (117 loc) · 3.14 KB
/
logadapter.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
package logadapter
import (
"fmt"
"log/slog"
"os"
"github.com/goware/logger"
"github.com/rs/zerolog"
)
func LogAdapter(log zerolog.Logger) logger.Logger {
return &logAdapter{log}
}
type logAdapter struct {
log zerolog.Logger
}
var _ logger.Logger = &logAdapter{}
func (s *logAdapter) With(args ...interface{}) logger.Logger {
return &logAdapter{s.log.With().Fields(args).Logger()}
}
func (s *logAdapter) Debug(v ...interface{}) {
msg, args := getArgs(v...)
withArgs(s.log.Debug(), args).Msg(msg)
}
func (s *logAdapter) Debugf(format string, v ...interface{}) {
s.log.Debug().Msg(fmt.Sprintf(format, v...))
}
func (s *logAdapter) Info(v ...interface{}) {
msg, args := getArgs(v...)
withArgs(s.log.Info(), args).Msg(msg)
}
func (s *logAdapter) Infof(format string, v ...interface{}) {
s.log.Info().Msg(fmt.Sprintf(format, v...))
}
func (s *logAdapter) Warn(v ...interface{}) {
msg, args := getArgs(v...)
withArgs(s.log.Warn(), args).Msg(msg)
}
func (s *logAdapter) Warnf(format string, v ...interface{}) {
s.log.Warn().Msg(fmt.Sprintf(format, v...))
}
func (s *logAdapter) Error(v ...interface{}) {
msg, args := getArgs(v...)
withArgs(s.log.Error(), args).Msg(msg)
}
func (s *logAdapter) Errorf(format string, v ...interface{}) {
s.log.Error().Msg(fmt.Sprintf(format, v...))
}
func (s *logAdapter) Fatal(v ...interface{}) {
msg, args := getArgs(v...)
withArgs(s.log.Fatal(), args).Msg(msg)
os.Exit(1)
}
func (s *logAdapter) Fatalf(format string, v ...interface{}) {
s.log.Fatal().Msg(fmt.Sprintf(format, v...))
os.Exit(1)
}
func (s *logAdapter) Print(v ...interface{}) {
s.log.Info().Msg(fmt.Sprint(v...))
}
func (s *logAdapter) Println(v ...interface{}) {
s.log.Info().Msg(fmt.Sprintln(v...))
}
func (s *logAdapter) Printf(format string, v ...interface{}) {
s.log.Info().Msg(fmt.Sprintf(format, v...))
}
// getArgs returns the message and arguments from the variadic arguments.
func getArgs(v ...interface{}) (string, []slog.Attr) {
if len(v) == 0 {
return "", nil
}
if len(v) == 1 {
return fmt.Sprint(v[0]), nil
}
// validate that the first argument is a string
msg, ok := v[0].(string)
if !ok {
return fmt.Sprint(v...), nil
}
args := make([]slog.Attr, 0, len(v)-1)
// validate that the rest of the arguments are Attr
for i := 1; i < len(v); i++ {
arg, ok := v[i].(slog.Attr)
if !ok {
return fmt.Sprint(v...), nil
}
args = append(args, arg)
}
return msg, args
}
func withArgs(logger *zerolog.Event, args []slog.Attr) *zerolog.Event {
for _, args := range args {
var value interface{}
switch t := args.Value.Kind(); t {
case slog.KindAny:
value = args.Value.Any()
case slog.KindBool:
value = args.Value.Bool()
case slog.KindDuration:
value = args.Value.Duration()
case slog.KindFloat64:
value = args.Value.Float64()
case slog.KindInt64:
value = args.Value.Int64()
case slog.KindString:
value = args.Value.String()
case slog.KindTime:
value = args.Value.Time()
case slog.KindUint64:
value = args.Value.Uint64()
case slog.KindGroup:
value = args.Value.Group()
case slog.KindLogValuer:
value = args.Value.LogValuer()
}
logger = logger.Interface(args.Key, value)
}
return logger
}