Newer
Older
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
package log
import (
"fmt"
"strings"
"time"
)
// A Formatter formats an entry.
type Formatter interface {
Format(*Entry) ([]byte, error)
}
// A FormatterFunc implements Formatter using a single function.
type FormatterFunc func(*Entry) ([]byte, error)
// Format implements Formatter.
func (f FormatterFunc) Format(entry *Entry) ([]byte, error) { return f(entry) }
// TextFormatter formats an entry as one-line text.
var TextFormatter = FormatterFunc(func(entry *Entry) (output []byte, err error) {
var builder strings.Builder
if _, err = fmt.Fprintf(
&builder,
"%s %s [%s]: %s",
entry.Time.Format(time.RFC3339),
entry.Category,
entry.Level,
entry.Message,
); err != nil {
return
}
entry.Fields.ForEach(func(name string, value interface{}) bool {
_, err = fmt.Fprintf(&builder, " %s=%v", name, value)
return err == nil
})
if err != nil {
return
}
if err = builder.WriteByte('\n'); err == nil {
output = []byte(builder.String())
}
return
})