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
|
package formatters
import (
"bytes"
"fmt"
"strings"
"github.com/mattermost/logr/v2"
)
// Plain is the simplest formatter, outputting only text with
// no colors.
type Plain struct {
// DisableTimestamp disables output of timestamp field.
DisableTimestamp bool `json:"disable_timestamp"`
// DisableLevel disables output of level field.
DisableLevel bool `json:"disable_level"`
// DisableMsg disables output of msg field.
DisableMsg bool `json:"disable_msg"`
// DisableFields disables output of all fields.
DisableFields bool `json:"disable_fields"`
// DisableStacktrace disables output of stack trace.
DisableStacktrace bool `json:"disable_stacktrace"`
// EnableCaller enables output of the file and line number that emitted a log record.
EnableCaller bool `json:"enable_caller"`
// Delim is an optional delimiter output between each log field.
// Defaults to a single space.
Delim string `json:"delim"`
// MinLevelLen sets the minimum level name length. If the level name is less
// than the minimum it will be padded with spaces.
MinLevelLen int `json:"min_level_len"`
// MinMessageLen sets the minimum msg length. If the msg text is less
// than the minimum it will be padded with spaces.
MinMessageLen int `json:"min_msg_len"`
// TimestampFormat is an optional format for timestamps. If empty
// then DefTimestampFormat is used.
TimestampFormat string `json:"timestamp_format"`
// LineEnd sets the end of line character(s). Defaults to '\n'.
LineEnd string `json:"line_end"`
// EnableColor sets whether output should include color.
EnableColor bool `json:"enable_color"`
}
func (p *Plain) CheckValid() error {
if p.MinMessageLen < 0 || p.MinMessageLen > 1024 {
return fmt.Errorf("min_msg_len is invalid(%d)", p.MinMessageLen)
}
return nil
}
// IsStacktraceNeeded returns true if a stacktrace is needed so we can output the `Caller` field.
func (p *Plain) IsStacktraceNeeded() bool {
return p.EnableCaller
}
// Format converts a log record to bytes.
func (p *Plain) Format(rec *logr.LogRec, level logr.Level, buf *bytes.Buffer) (*bytes.Buffer, error) {
delim := p.Delim
if delim == "" {
delim = " "
}
if buf == nil {
buf = &bytes.Buffer{}
}
timestampFmt := p.TimestampFormat
if timestampFmt == "" {
timestampFmt = logr.DefTimestampFormat
}
color := logr.NoColor
if p.EnableColor {
color = level.Color
}
if !p.DisableLevel {
_ = logr.WriteWithColor(buf, level.Name, color)
count := len(level.Name)
if p.MinLevelLen > count {
_, _ = buf.WriteString(strings.Repeat(" ", p.MinLevelLen-count))
}
buf.WriteString(delim)
}
if !p.DisableTimestamp {
var arr [128]byte
tbuf := rec.Time().AppendFormat(arr[:0], timestampFmt)
buf.WriteByte('[')
buf.Write(tbuf)
buf.WriteByte(']')
buf.WriteString(delim)
}
if !p.DisableMsg {
count, _ := buf.WriteString(rec.Msg())
if p.MinMessageLen > count {
_, _ = buf.WriteString(strings.Repeat(" ", p.MinMessageLen-count))
}
_, _ = buf.WriteString(delim)
}
var fields []logr.Field
if p.EnableCaller {
fld := logr.Field{
Key: "caller",
Type: logr.StringType,
String: rec.Caller(),
}
fields = append(fields, fld)
}
if !p.DisableFields {
fields = append(fields, rec.Fields()...)
}
if len(fields) > 0 {
if err := logr.WriteFields(buf, fields, logr.Space, color); err != nil {
return nil, err
}
}
if level.Stacktrace && !p.DisableStacktrace {
frames := rec.StackFrames()
if len(frames) > 0 {
buf.WriteString("\n")
if err := logr.WriteStacktrace(buf, rec.StackFrames()); err != nil {
return nil, err
}
}
}
if p.LineEnd == "" {
buf.WriteString("\n")
} else {
buf.WriteString(p.LineEnd)
}
return buf, nil
}
|