forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.go
143 lines (121 loc) · 3.2 KB
/
wrap.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
package log
// PrintFormatter is an interface implemented by standard loggers.
type PrintFormatter interface {
Printf(format string, v ...any)
Println(v ...any)
}
//------------------------------------------------------------------------------
// Logger level constants.
const (
LogOff int = 0
LogFatal int = 1
LogError int = 2
LogWarn int = 3
LogInfo int = 4
LogDebug int = 5
LogTrace int = 6
LogAll int = 7
)
// wrapped is an object with support for levelled logging and modular components.
type wrapped struct {
pf PrintFormatter
level int
}
// Wrap a PrintFormatter with a log.Modular implementation. Log level is set to
// INFO, use WrapAtLevel to set this explicitly.
func Wrap(l PrintFormatter) Modular {
return &wrapped{
pf: l,
level: LogInfo,
}
}
// WrapAtLevel wraps a PrintFormatter with a log.Modular implementation with an
// explicit log level.
func WrapAtLevel(l PrintFormatter, level int) Modular {
return &wrapped{
pf: l,
level: level,
}
}
//------------------------------------------------------------------------------
// WithFields is a no-op.
func (l *wrapped) WithFields(fields map[string]string) Modular {
return l
}
// With is a no-op.
func (l *wrapped) With(keyValues ...any) Modular {
return l
}
// Fatalf prints a fatal message to the console. Does NOT cause panic.
func (l *wrapped) Fatalf(format string, v ...any) {
if LogFatal <= l.level {
l.pf.Printf(format, v...)
}
}
// Errorf prints an error message to the console.
func (l *wrapped) Errorf(format string, v ...any) {
if LogError <= l.level {
l.pf.Printf(format, v...)
}
}
// Warnf prints a warning message to the console.
func (l *wrapped) Warnf(format string, v ...any) {
if LogWarn <= l.level {
l.pf.Printf(format, v...)
}
}
// Infof prints an information message to the console.
func (l *wrapped) Infof(format string, v ...any) {
if LogInfo <= l.level {
l.pf.Printf(format, v...)
}
}
// Debugf prints a debug message to the console.
func (l *wrapped) Debugf(format string, v ...any) {
if LogDebug <= l.level {
l.pf.Printf(format, v...)
}
}
// Tracef prints a trace message to the console.
func (l *wrapped) Tracef(format string, v ...any) {
if LogTrace <= l.level {
l.pf.Printf(format, v...)
}
}
//------------------------------------------------------------------------------
// Fatalln prints a fatal message to the console. Does NOT cause panic.
func (l *wrapped) Fatalln(message string) {
if LogFatal <= l.level {
l.pf.Println(message)
}
}
// Errorln prints an error message to the console.
func (l *wrapped) Errorln(message string) {
if LogError <= l.level {
l.pf.Println(message)
}
}
// Warnln prints a warning message to the console.
func (l *wrapped) Warnln(message string) {
if LogWarn <= l.level {
l.pf.Println(message)
}
}
// Infoln prints an information message to the console.
func (l *wrapped) Infoln(message string) {
if LogInfo <= l.level {
l.pf.Println(message)
}
}
// Debugln prints a debug message to the console.
func (l *wrapped) Debugln(message string) {
if LogDebug <= l.level {
l.pf.Println(message)
}
}
// Traceln prints a trace message to the console.
func (l *wrapped) Traceln(message string) {
if LogTrace <= l.level {
l.pf.Println(message)
}
}