-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
format.go
163 lines (137 loc) · 3.65 KB
/
format.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
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
// Package logger 日志包,用于日志输出和打印
package logger
import (
"bytes"
"fmt"
"runtime"
"sort"
"time"
"github.com/gookit/color"
"github.com/sirupsen/logrus"
)
// LogFileFormatter defines the format for log file.
type LogFileFormatter struct {
logrus.TextFormatter
MinimumCallerDepth int
}
// NewLogFileFormatter return the log format for log file.
//
// eg: 2023-06-01T12:00:00 [info] [controllers/some_controller/code_file.go:99] foo key=value
func NewLogFileFormatter() *LogFileFormatter {
return &LogFileFormatter{
TextFormatter: logrus.TextFormatter{
TimestampFormat: time.RFC3339Nano,
FullTimestamp: true,
},
MinimumCallerDepth: 0,
}
}
// Format renders a single log entry for log file
//
// the original file log format is defined here: github.com/sirupsen/logrus/text_formatter.TextFormatter{}.Format().
func (f *LogFileFormatter) Format(entry *logrus.Entry) ([]byte, error) {
data := make(map[string]any)
for k, v := range entry.Data {
data[k] = v
}
keys := make([]string, 0, len(data))
for k := range data {
if k == "file" {
continue
}
keys = append(keys, k)
}
if !f.DisableSorting {
if nil != f.SortingFunc {
f.SortingFunc(keys)
} else {
sort.Strings(keys)
}
}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = time.RFC3339
}
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
prefixStr := entry.Time.Format(timestampFormat) + " "
var renderFunc func(a ...any) string
switch entry.Level {
case logrus.TraceLevel:
renderFunc = color.FgGray.Render
case logrus.DebugLevel:
renderFunc = color.FgGreen.Render
case logrus.InfoLevel:
renderFunc = color.FgCyan.Render
case logrus.WarnLevel:
renderFunc = color.FgYellow.Render
case logrus.ErrorLevel:
renderFunc = color.FgRed.Render
case logrus.FatalLevel:
renderFunc = color.FgMagenta.Render
case logrus.PanicLevel:
renderFunc = color.FgMagenta.Render
default:
renderFunc = color.FgGray.Render
}
prefixStr += renderFunc("[", entry.Level.String(), "]")
b.WriteString(prefixStr)
if data["file"] != nil {
b.WriteString(fmt.Sprintf(" [%s]", data["file"]))
delete(data, "file")
} else if entry.Context != nil {
caller, _ := entry.Context.Value(runtimeCaller).(*runtime.Frame)
if caller != nil {
b.WriteString(fmt.Sprintf(" [%s:%d]", caller.File, caller.Line))
}
}
if "" != entry.Message {
b.WriteString(" " + entry.Message)
}
for _, key := range keys {
value := data[key]
appendKeyValue(b, key, value, f.QuoteEmptyFields)
}
b.WriteByte('\n')
return b.Bytes(), nil
}
// appendKeyValue append value with key to data that to be appended to log file.
func appendKeyValue(b *bytes.Buffer, key string, value interface{}, QuoteEmptyFields bool) {
if b.Len() > 0 {
b.WriteByte(' ')
}
b.WriteString(key)
b.WriteByte('=')
appendValue(b, value, QuoteEmptyFields)
}
// appendValue append value to data used for method appendKeyValue.
func appendValue(b *bytes.Buffer, value interface{}, QuoteEmptyFields bool) {
stringVal, ok := value.(string)
if !ok {
stringVal = fmt.Sprint(value)
}
if !needsQuoting(stringVal, QuoteEmptyFields) {
b.WriteString(stringVal)
} else {
b.WriteString(fmt.Sprintf("%q", stringVal))
}
}
// needsQuoting check where text needs to be quoted.
func needsQuoting(text string, QuoteEmptyFields bool) bool {
if QuoteEmptyFields && len(text) == 0 {
return true
}
for _, ch := range text {
if !((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
return true
}
}
return false
}