-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
117 lines (107 loc) · 2.59 KB
/
util.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
package golog
// LvlToString will take the given log level and
// return the string that represents it
//
// Note that this function can only translate default
// log levels
func LvlToString(lvl uint64) string {
switch lvl {
case LvlTrace:
return "TRACE"
case LvlDebug:
return "DEBUG"
case LvlInfo:
return "INFO"
case LvlWarn:
return "WARN"
case LvlError:
return "ERROR"
case LvlFatal:
return "FATAL"
default:
return "????"
}
}
// ColorizeStrByLvl will colorize the log msg using the
// ANSI color code associated with the log level
func ColorizeStrByLvl(lvl uint64, msg string) string {
switch lvl {
case LvlTrace:
return DarkGreyString(msg)
case LvlDebug:
return LightGreyString(msg)
case LvlInfo:
return CyanString(msg)
case LvlWarn:
return YellowString(msg)
case LvlError:
return RedString(msg)
case LvlFatal:
return BoldRedString(msg)
default:
return msg
}
}
// -----
// tryRead will read the given key from some LogFields,
// returning nil if it's not present
//
// Note that the 'f' param is a variadic just to sugar the
// syntax (only the first index is used)
func tryRead(key string, f ...LogFields) interface{} {
if len(f) == 0 {
return nil
}
var v interface{}
for _, fields := range f {
value, exists := fields[key]
if exists {
v = value
}
}
return v
}
// cloneOrNew will create a new Map and
// copy all values from the given param. If nil/empty,
// returns a new empty Map
func cloneOrNew[T any](m map[string]T) map[string]T {
n := make(map[string]T, len(m))
for key, value := range m {
n[key] = value
}
return n
}
// mergeOverriding will copy the values from all srcs to dest,
// overriding any existing values, in order
func mergeOverriding[T any](dest map[string]T, srcs ...map[string]T) {
if len(srcs) == 0 {
return
}
for _, fields := range srcs {
for key, value := range fields {
dest[key] = value
}
}
}
// applyHooks will call the given hooks and set the returned
// values to the given fields (using the given log as a parameter)
func applyHooks(log Log, fields LogFields, hooks Hooks) {
for key, hook := range hooks {
fields[key] = hook(log)
}
}
// notEnabled will return true if the given log level is not enabled by the
// given flags
func notEnabled(flags uint64, logLvl uint64) bool { return (flags & logLvl) == 0 }
// cloneLogger will create a new identical Logger
// instance from the given one
func cloneLogger(l *logger) *logger {
outputs := make([]Output, len(l.outputs))
copy(outputs, l.outputs)
return &logger{
l.configuration,
cloneOrNew(l.fields),
cloneOrNew(l.preHooks),
cloneOrNew(l.postHooks),
outputs}
}