Repository navigation
Expand file tree
/
Copy pathlog.go
More file actions
280 lines (240 loc) · 7.54 KB
/
Copy pathlog.go
File metadata and controls
280 lines (240 loc) · 7.54 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Package log implements a minimalistic logging library.
package log
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/foxcpp/maddy/framework/exterrors"
"go.uber.org/zap"
)
// Logger is the structure that writes formatted output to the underlying
// log.Output object.
//
// Logger is stateless and can be copied freely. However, consider that
// underlying log.Output will not be copied.
//
// Each log message is prefixed with logger name. Timestamp and debug flag
// formatting is done by log.Output.
//
// No serialization is provided by Logger, its log.Output responsibility to
// ensure goroutine-safety if necessary.
type Logger struct {
Parent *Logger
Out Output
Name string
Debug bool
// Additional fields that will be added
// to the Msg output.
Fields map[string]interface{}
}
func (l *Logger) Zap() *zap.Logger {
// TODO: Migrate to using zap natively.
return zap.New(zapLogger{L: l})
}
func (l *Logger) IsDebug() bool {
return l.Debug || (l.Parent != nil && l.Parent.IsDebug())
}
func (l *Logger) Debugf(format string, val ...interface{}) {
if !l.IsDebug() {
return
}
l.log(true, l.formatMsg(fmt.Sprintf(format, val...), nil))
}
func (l *Logger) Debugln(val ...interface{}) {
if !l.IsDebug() {
return
}
l.log(true, l.formatMsg(strings.TrimRight(fmt.Sprintln(val...), "\n"), nil))
}
func (l *Logger) Printf(format string, val ...interface{}) {
l.log(false, l.formatMsg(fmt.Sprintf(format, val...), nil))
}
func (l *Logger) Println(val ...interface{}) {
l.log(false, l.formatMsg(strings.TrimRight(fmt.Sprintln(val...), "\n"), nil))
}
// Msg writes an event log message in a machine-readable format (currently
// JSON).
//
// name: msg\t{"key":"value","key2":"value2"}
//
// Key-value pairs are built from fields slice which should contain key strings
// followed by corresponding values. That is, for example, []interface{"key",
// "value", "key2", "value2"}.
//
// If value in fields implements Formatter, it will be represented by the
// string returned by FormatLog method. Same goes for fmt.Stringer and error
// interfaces.
//
// Additionally, time.Time is written as a string in ISO 8601 format.
// time.Duration follows fmt.Stringer rule above.
func (l *Logger) Msg(msg string, fields ...interface{}) {
m := make(map[string]interface{}, len(fields)/2)
fieldsToMap(fields, m)
l.log(false, l.formatMsg(msg, m))
}
// Error writes an event log message in a machine-readable format (currently
// JSON) containing information about the error. If err does have a Fields
// method that returns map[string]interface{}, its result will be added to the
// message.
//
// name: msg\t{"key":"value","key2":"value2"}
//
// Additionally, values from fields will be added to it, as handled by
// Logger.Msg.
//
// In the context of Error method, "msg" typically indicates the top-level
// context in which the error is *handled*. For example, if error leads to
// rejection of SMTP DATA command, msg will probably be "DATA error".
func (l *Logger) Error(msg string, err error, fields ...interface{}) {
if err == nil {
return
}
errFields := exterrors.Fields(err)
allFields := make(map[string]interface{}, len(fields)+len(errFields)+2)
for k, v := range errFields {
allFields[k] = v
}
// If there is already a 'reason' field - use it, it probably
// provides a better explanation than error text itself.
if allFields["reason"] == nil {
allFields["reason"] = err.Error()
}
fieldsToMap(fields, allFields)
l.log(false, l.formatMsg(msg, allFields))
}
func (l *Logger) DebugMsg(kind string, fields ...interface{}) {
if !l.IsDebug() {
return
}
m := make(map[string]interface{}, len(fields)/2)
fieldsToMap(fields, m)
l.log(true, l.formatMsg(kind, m))
}
func fieldsToMap(fields []interface{}, out map[string]interface{}) {
var lastKey string
for i, val := range fields {
if i%2 == 0 {
// Key
key, ok := val.(string)
if !ok {
// Misformatted arguments, attempt to provide useful message
// anyway.
out[fmt.Sprint("field", i)] = key
continue
}
lastKey = key
} else {
// Value
out[lastKey] = val
}
}
}
func (l *Logger) formatMsg(msg string, fields map[string]interface{}) string {
formatted := strings.Builder{}
formatted.WriteString(msg)
formatted.WriteRune('\t')
if len(l.Fields)+len(fields) != 0 {
if fields == nil {
fields = make(map[string]interface{})
}
for k, v := range l.Fields {
fields[k] = v
}
if err := marshalOrderedJSON(&formatted, fields); err != nil {
// Fallback to printing the message with minimal processing.
return fmt.Sprintf("[BROKEN FORMATTING: %v] %v %+v", err, msg, fields)
}
}
return formatted.String()
}
type Formatter interface {
FormatLog() string
}
// Write implements io.Writer, all bytes sent
// to it will be written as a separate log messages.
// No line-buffering is done.
func (l *Logger) Write(s []byte) (int, error) {
if !l.IsDebug() {
return len(s), nil
}
l.log(false, strings.TrimRight(string(s), "\n"))
return len(s), nil
}
// Close closes underlying output in Out.
func (l *Logger) Close() error {
if l.Out == nil {
return nil
}
return l.Out.Close()
}
// DebugWriter returns a writer that will act like Logger.Write
// but will use debug flag on messages. If Logger.Debug is false,
// Write method of returned object will be no-op.
func (l *Logger) DebugWriter() io.Writer {
l2 := l.Sublogger("")
l2.Debug = true
return l2
}
func (l *Logger) output() Output {
if l.Out != nil {
return l.Out
}
if l.Parent != nil {
return l.Parent.output()
}
if DefaultLogger.Out == nil {
panic("DefaultLogger.Out is not set")
}
if l.Parent == nil && l != &DefaultLogger {
DefaultLogger.Out.Write(time.Now(), true, "logger "+l.Name+" has no parent, this is a bug")
}
return DefaultLogger.Out
}
func (l *Logger) log(debug bool, s string) {
if l.Name != "" {
s = l.Name + ": " + s
}
out := l.output()
out.Write(time.Now(), debug, s)
// Logging is disabled - do nothing.
}
func (l *Logger) Sublogger(name string) *Logger {
if l.Name != "" && name != "" {
name = l.Name + "/" + name
}
return &Logger{
Parent: l,
Name: name,
}
}
// DefaultLogger is the global Logger object that is used by
// package-level logging functions.
//
// As with all other Loggers, it is not gorountine-safe on its own,
// however underlying log.Output may provide necessary serialization.
var DefaultLogger = Logger{Out: WriterOutput(os.Stderr, false)}
// NopLogger is the logger that discards all messages written to it.
var NopLogger = Logger{
Parent: &DefaultLogger,
Out: NopOutput{},
}
func Debugf(format string, val ...interface{}) { DefaultLogger.Debugf(format, val...) }
func Debugln(val ...interface{}) { DefaultLogger.Debugln(val...) }
func Printf(format string, val ...interface{}) { DefaultLogger.Printf(format, val...) }
func Println(val ...interface{}) { DefaultLogger.Println(val...) }