-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc.go
69 lines (57 loc) · 1.47 KB
/
func.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
package formatter
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
)
//nolint:gochecknoglobals // used as constants
var (
emojisLevel = [7]string{"💀", "🤬", "💩", "🙈", "🙃", "🤷", "🤮"}
colors = [7]string{"[44;1m", "[31;1m", "[31;1m", "[33m", "[36m", "[37;1m", "[35;1m"}
)
const (
logFieldColor = "[35;1m"
start = "\033"
end = start + "[0m"
)
// Format building log message.
func (f *Config) Format(entry *logrus.Entry) ([]byte, error) {
format := f.LogFormat
if format == "" {
format = defaultLogFormat
}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = defaultTimestampFormat
}
level := strings.ToUpper(entry.Level.String())
i, _ := logrus.ParseLevel(level)
emoji := emojisLevel[i]
l := level
m := entry.Message
fieldPattern := "%s"
if f.Color {
color := colors[i]
l = start + color + level + end
m = start + color + entry.Message + end
fieldPattern = start + logFieldColor + fieldPattern + end
}
replacer := strings.NewReplacer(
"%time%", entry.Time.Format(timestampFormat),
"%msg%", m,
"%lvl%", l,
"%emoji%", emoji,
)
output := replacer.Replace(format)
for k, val := range entry.Data {
switch val := val.(type) {
case []string:
v := strings.Join(val, "\n\t - ")
output += fmt.Sprintf("\n\t"+fieldPattern+": \n\t - %v", k, v)
default:
output += fmt.Sprintf("\n\t"+fieldPattern+": %v", k, val)
}
}
output += "\n"
return []byte(output), nil
}