-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
122 lines (105 loc) · 3.19 KB
/
common.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
package logger
import (
"context"
"io"
"os"
"time"
"github.com/mgutz/ansi"
"golang.org/x/term"
)
const delimiter = "."
var void = context.Background()
func checkIfTerminal(w io.Writer) bool {
switch v := w.(type) {
case *os.File:
return term.IsTerminal(int(v.Fd()))
default:
return false
}
}
// This is to not silently overwrite `time`, `msg` and `level` fields when
// dumping it. If this code wasn't there doing:
//
// logrus.WithField("level", 1).Info("hello")
//
// would just silently drop the user provided level. Instead with this code
// it'll be logged as:
//
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
func prefixFieldClashes(data map[string]any) {
if t, ok := data["time"]; ok {
data["fields.time"] = t
}
if m, ok := data["msg"]; ok {
data["fields.msg"] = m
}
if l, ok := data["level"]; ok {
data["fields.level"] = l
}
}
const defaultTimestampFormat = time.RFC3339
var (
baseTimestamp = time.Now()
defaultColorScheme = &ColorScheme{
InfoLevelStyle: "green",
WarnLevelStyle: "yellow",
ErrorLevelStyle: "red",
FatalLevelStyle: "red",
PanicLevelStyle: "red",
DebugLevelStyle: "blue",
PrefixStyle: "cyan",
TimestampStyle: "black+h",
}
noColorsColorScheme = &compiledColorScheme{
InfoLevelColor: ansi.ColorFunc(""),
WarnLevelColor: ansi.ColorFunc(""),
ErrorLevelColor: ansi.ColorFunc(""),
FatalLevelColor: ansi.ColorFunc(""),
PanicLevelColor: ansi.ColorFunc(""),
DebugLevelColor: ansi.ColorFunc(""),
PrefixColor: ansi.ColorFunc(""),
TimestampColor: ansi.ColorFunc(""),
}
defaultCompiledColorScheme = compileColorScheme(defaultColorScheme)
)
func miniTS() int {
return int(time.Since(baseTimestamp) / time.Second)
}
type ColorScheme struct {
InfoLevelStyle string
WarnLevelStyle string
ErrorLevelStyle string
FatalLevelStyle string
PanicLevelStyle string
DebugLevelStyle string
PrefixStyle string
TimestampStyle string
}
type compiledColorScheme struct {
InfoLevelColor func(string) string
WarnLevelColor func(string) string
ErrorLevelColor func(string) string
FatalLevelColor func(string) string
PanicLevelColor func(string) string
DebugLevelColor func(string) string
PrefixColor func(string) string
TimestampColor func(string) string
}
func getCompiledColor(main string, fallback string) func(string) string {
if main == "" {
main = fallback
}
return ansi.ColorFunc(main)
}
func compileColorScheme(s *ColorScheme) *compiledColorScheme {
return &compiledColorScheme{
InfoLevelColor: getCompiledColor(s.InfoLevelStyle, defaultColorScheme.InfoLevelStyle),
WarnLevelColor: getCompiledColor(s.WarnLevelStyle, defaultColorScheme.WarnLevelStyle),
ErrorLevelColor: getCompiledColor(s.ErrorLevelStyle, defaultColorScheme.ErrorLevelStyle),
FatalLevelColor: getCompiledColor(s.FatalLevelStyle, defaultColorScheme.FatalLevelStyle),
PanicLevelColor: getCompiledColor(s.PanicLevelStyle, defaultColorScheme.PanicLevelStyle),
DebugLevelColor: getCompiledColor(s.DebugLevelStyle, defaultColorScheme.DebugLevelStyle),
PrefixColor: getCompiledColor(s.PrefixStyle, defaultColorScheme.PrefixStyle),
TimestampColor: getCompiledColor(s.TimestampStyle, defaultColorScheme.TimestampStyle),
}
}