-
Notifications
You must be signed in to change notification settings - Fork 13
/
formatter.go
93 lines (80 loc) · 1.83 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
87
88
89
90
91
92
93
package main
import (
"bytes"
"fmt"
"os"
)
import "github.com/sirupsen/logrus"
import "golang.org/x/crypto/ssh/terminal"
const (
ANSI_RESET = "\033[0m"
ANSI_BOLD = "\033[1m"
ANSI_CYAN = "\033[96m"
ANSI_MAGENTA = "\033[95m"
ANSI_RED = "\033[91m"
ANSI_YELLOW = "\033[93m"
ANSI_BLUE = "\033[94m"
ANSI_GREEN = "\033[92m"
ANSI_WHITE = "\033[97m"
)
type fluentBitLogFormat struct{}
//Format Specify logging format.
func (f *fluentBitLogFormat) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
bold_color := ANSI_BOLD
reset_color := ANSI_RESET
header_title := ""
header_color := ""
switch entry.Level {
case logrus.TraceLevel:
header_title = "trace"
header_color = ANSI_BLUE
case logrus.InfoLevel:
header_title = "info"
header_color = ANSI_GREEN
case logrus.WarnLevel:
header_title = "warn"
header_color = ANSI_YELLOW
case logrus.ErrorLevel:
header_title = "error"
header_color = ANSI_RED
case logrus.DebugLevel:
header_title = "debug"
header_color = ANSI_YELLOW
case logrus.FatalLevel:
header_title = "fatal"
header_color = ANSI_MAGENTA
}
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
header_color = ""
bold_color = ""
reset_color = ""
}
time := fmt.Sprintf("%s[%s%s%s]%s",
bold_color, reset_color,
entry.Time.Format("2006/01/02 15:04:05"),
bold_color, reset_color)
b.WriteString(time)
level := fmt.Sprintf(" [%s%5s%s] ", header_color, header_title, reset_color)
b.WriteString(level)
if entry.Message != "" {
b.WriteString(entry.Message)
}
if len(entry.Data) > 0 {
b.WriteString(" || ")
}
for key, value := range entry.Data {
b.WriteString(key)
b.WriteByte('=')
b.WriteByte('{')
fmt.Fprint(b, value)
b.WriteString("}, ")
}
b.WriteByte('\n')
return b.Bytes(), nil
}