-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
kzerolog.go
68 lines (62 loc) · 1.67 KB
/
kzerolog.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
// Package kzerolog provides a plug-in kgo.Logger wrapping zerolog.Logger for usage in
// a kgo.Client.
//
// This can be used like so:
//
// cl, err := kgo.NewClient(
// kgo.WithLogger(kzerolog.New(logger)),
// // ...other opts
// )
package kzerolog
import (
"github.com/rs/zerolog"
"github.com/twmb/franz-go/pkg/kgo"
)
// Logger provides the kgo.Logger interface for usage in kgo.WithLogger when
// initializing a client.
type Logger struct {
zl *zerolog.Logger
}
// New returns a new logger.
func New(zl *zerolog.Logger) *Logger {
return &Logger{zl}
}
// Level is for the kgo.Logger interface.
func (l *Logger) Level() kgo.LogLevel {
level := l.zl.GetLevel()
switch level {
case zerolog.ErrorLevel, zerolog.PanicLevel, zerolog.FatalLevel:
return kgo.LogLevelError
case zerolog.WarnLevel:
return kgo.LogLevelWarn
case zerolog.InfoLevel:
return kgo.LogLevelInfo
case zerolog.TraceLevel, zerolog.DebugLevel:
return kgo.LogLevelDebug
case zerolog.NoLevel, zerolog.Disabled:
return kgo.LogLevelNone
default:
return kgo.LogLevelNone
}
}
// Log is for the kgo.Logger interface.
func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) {
l.zl.WithLevel(logLevelToZerolog(level)).Fields(keyvals).Msg(msg)
}
func logLevelToZerolog(level kgo.LogLevel) zerolog.Level {
switch level {
case kgo.LogLevelError:
return zerolog.ErrorLevel
case kgo.LogLevelWarn:
return zerolog.WarnLevel
case kgo.LogLevelInfo:
return zerolog.InfoLevel
case kgo.LogLevelDebug:
return zerolog.DebugLevel
case kgo.LogLevelNone:
return zerolog.Disabled
default:
// Follows the default of zerolog.New of defaulting to TraceLevel.
return zerolog.TraceLevel
}
}