-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
61 lines (50 loc) · 1.15 KB
/
tools.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
package log
import (
"fmt"
"time"
)
// The activeLevels returns only active level list.
func activeLevels(m map[Level]bool) []Level {
var result = make([]Level, 0, len(m))
for key, value := range m {
if value {
result = append(result, key)
}
}
return result
}
// The getPrefix creates a log message prefix based on the collected call
// stack data, time and log level.
func getPrefix(trace *Trace, format, timestampFormat string, level Level,
showFilePath, showFuncName, showFileLine bool) string {
var path, name, line string
timestamp := time.Now().Format(timestampFormat)
if showFilePath {
path = fmt.Sprintf("%s ", trace.FilePath)
}
if showFuncName {
name = trace.FuncName
if showFileLine {
name += ":"
} else {
name += " "
}
}
if showFileLine {
line = fmt.Sprintf("%d ", trace.FileLine)
}
r := fmt.Sprintf("%s [%s] %s%s%s", timestamp, level, path, name, line)
if len(format) > 0 {
r += format
}
return r
}
// The in function returns true if levels contains specified log level.
func in(level Level, levels ...Level) bool {
for _, item := range levels {
if level == item {
return true
}
}
return false
}