-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner_text_formatter.go
76 lines (63 loc) · 1.7 KB
/
runner_text_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
package formatter
import (
"bytes"
"fmt"
"sort"
"github.com/Sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
)
type RunnerTextFormatter struct {
// Force disabling colors.
DisableColors bool
// The fields are sorted by default for a consistent output. For applications
// that log extremely frequently and don't use the JSON formatter this may not
// be desired.
DisableSorting bool
}
func (f *RunnerTextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var keys = make([]string, 0, len(entry.Data))
for k := range entry.Data {
keys = append(keys, k)
}
if !f.DisableSorting {
sort.Strings(keys)
}
b := bytes.Buffer{}
f.printColored(&b, entry, keys)
b.WriteByte('\n')
return b.Bytes(), nil
}
func (f *RunnerTextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys []string) {
var levelColor, levelText string
switch entry.Level {
case logrus.DebugLevel:
levelColor = helpers.ANSI_BOLD_WHITE
case logrus.WarnLevel:
levelColor = helpers.ANSI_YELLOW
levelText = "WARNING: "
case logrus.ErrorLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "ERROR: "
case logrus.FatalLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "FATAL: "
case logrus.PanicLevel:
levelColor = helpers.ANSI_BOLD_RED
levelText = "PANIC: "
default:
}
resetColor := helpers.ANSI_RESET
if f.DisableColors {
levelColor = ""
resetColor = ""
}
indentLength := 50 - len(levelText)
fmt.Fprintf(b, "%s%s%-*s%s ", levelColor, levelText, indentLength, entry.Message, resetColor)
for _, k := range keys {
v := entry.Data[k]
fmt.Fprintf(b, " %s%s%s=%v", levelColor, k, resetColor, v)
}
}
func SetRunnerFormatter() {
logrus.SetFormatter(&RunnerTextFormatter{})
}