-
Notifications
You must be signed in to change notification settings - Fork 272
/
formatter.go
86 lines (68 loc) · 2.04 KB
/
formatter.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
package logger
import (
"fmt"
"sort"
"strings"
)
// Formatter defines the rules on how to format the logging context before
// transport. For example, a Formatter might prepare the context for writing to
// a log file, or serialize it to JSON before sending the bytes to transport.
type Formatter interface {
// Format formats the logging context for transport.
Format(message Message) ([]byte, error)
}
// StringFormatter is the default implementation of Formatter and it prepares
// the ctx for printing to stdout/stderr or a file.
type StringFormatter struct {
params *StringFormatterParams
}
// StringFormatterParams are parameters for StringFormatter.
type StringFormatterParams struct {
// DateLayout is the layout to be passed to time.Time.Format function for
// formatting logging timestamp.
DateLayout string
// DisableContextKeySorting will not sort context keys before printing them.
DisableContextKeySorting bool
}
// compile-time assertion that StringFormatter implements Formatter.
var _ Formatter = &StringFormatter{}
// NewStringFormatter creates a new instance of StringFormatter.
func NewStringFormatter(params StringFormatterParams) *StringFormatter {
if params.DateLayout == "" {
params.DateLayout = "2006-01-02T15:04:05.000000Z07:00"
}
return &StringFormatter{
params: ¶ms,
}
}
// Format implements Formatter.
func (f *StringFormatter) Format(message Message) ([]byte, error) {
// TODO maybe use sync.Pool for builders here.
ctx := message.Ctx
var keys []string
if l := len(ctx); l > 0 {
keys = make([]string, 0, l)
for k := range ctx {
keys = append(keys, k)
}
}
if !f.params.DisableContextKeySorting {
sort.Strings(keys)
}
var b strings.Builder
for _, k := range keys {
v := ctx[k]
b.WriteString(" ")
b.WriteString(k)
b.WriteString("=")
b.WriteString(fmt.Sprintf("%+v", v))
}
ret := fmt.Sprintf("%s %5s [%20s] %s%s\n",
message.Timestamp.Format(f.params.DateLayout),
message.Level,
message.Namespace,
strings.TrimRight(message.Body, "\n"),
b.String(),
)
return []byte(ret), nil
}