-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.go
More file actions
133 lines (108 loc) · 2.65 KB
/
formatter.go
File metadata and controls
133 lines (108 loc) · 2.65 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
package formatter
import (
"bytes"
"fmt"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
const (
defaultTimestampFormat = time.RFC3339
)
var _ log.Formatter = &AppFormatter{}
type AppFormatter struct {
DisableTimestamp bool
DisablePID bool
}
// Format implements interface
func (f *AppFormatter) Format(entry *log.Entry) ([]byte, error) {
// initial buffer if necessary
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
// output fixed fields
// timestamp
if !f.DisableTimestamp {
f.appendWithoutKey(b, entry.Time.Local().Format(defaultTimestampFormat))
}
// log level
f.appendFieldLevel(b, entry.Level.String())
// process id
if !f.DisablePID {
f.appendProcessID(b, os.Getpid())
}
if entry.HasCaller() {
// function field
f.appendBracketsValue(b, path.Base(entry.Caller.Function)+"()")
// file field
f.appendBracketsValue(b, path.Base(entry.Caller.File)+":"+strconv.Itoa(entry.Caller.Line))
}
// message
trimmed := strings.TrimSpace(entry.Message)
if trimmed != "" {
f.appendWithoutKey(b, trimmed)
}
// sorting additional fields
keys := make([]string, 0, len(entry.Data))
for k := range entry.Data {
// remove the key-value pair which key is an empty or space string.
// to avoid from the weird log text: " = xxx value" which has no key string
t := strings.TrimSpace(k)
if t == "" {
continue
}
keys = append(keys, k)
}
sort.Strings(keys)
// output additional fields
for _, k := range keys {
f.appendKeyValue(b, k, entry.Data[k])
}
// given line break at the end
b.WriteByte('\n')
return b.Bytes(), nil
}
func (f *AppFormatter) appendWithoutKey(b *bytes.Buffer, value interface{}) {
if b.Len() > 0 {
b.WriteByte(' ')
}
f.appendValue(b, value)
}
func (f *AppFormatter) appendFieldLevel(b *bytes.Buffer, value interface{}) {
f.appendBracketsValue(b, fmt.Sprintf("%s", strings.ToUpper(value.(string))[:4]))
}
func (f *AppFormatter) appendProcessID(b *bytes.Buffer, value int) {
f.appendBracketsValue(b, fmt.Sprintf("%.4x", value))
}
func (f *AppFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
if b.Len() > 0 {
b.WriteByte(' ')
}
b.WriteByte('[')
b.WriteString(key)
b.WriteByte(':')
f.appendValue(b, value)
b.WriteByte(']')
}
func (f *AppFormatter) appendBracketsValue(b *bytes.Buffer, value interface{}) {
if b.Len() > 0 {
b.WriteByte(' ')
}
b.WriteByte('[')
f.appendValue(b, value)
b.WriteByte(']')
}
func (f *AppFormatter) appendValue(b *bytes.Buffer, value interface{}) {
stringVal, ok := value.(string)
if !ok {
stringVal = fmt.Sprint(value)
}
b.WriteString(stringVal)
}