-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
96 lines (76 loc) · 1.67 KB
/
write.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
package timber
import (
"fmt"
"os"
"strings"
"time"
)
func Write(level int, args ...interface{}) {
if len(writers) == 0 {
if defaultWriter == nil {
defaultWriter = os.Stdout
}
fmt.Fprintln(defaultWriter, colorError+"timber.Write Error: no log writers defined, defaulting to os.Stdout"+colorReset)
newWriter := options{
flags: FlagNone,
level: LevelAll,
timestamp: "[15:04:05]",
writer: defaultWriter,
}
writers = append(writers, newWriter)
} else if level < LevelCritical {
return
}
levelInfo := getLevelInfo(level)
for _, w := range writers {
if level > w.level {
continue
}
var message string
flagColorful := w.flags&FlagColorful != 0
flagFile := w.flags&FlagFileName | w.flags&FlagFileProject | w.flags&FlagFileAbsolute
flagTitle := w.flags&FlagTitle != 0
// timestamp
if w.timestamp != "" {
if flagColorful {
message += colorTime
}
message += fmt.Sprintf("%s ", time.Now().Format(w.timestamp))
}
// filename
if flagFile != 0 {
file := fileWithLineNum(flagFile)
if file != "" {
message += fmt.Sprintf("%s ", file)
}
}
// title
if flagColorful {
message += levelInfo.color
}
if flagTitle {
message += fmt.Sprintf("%s ", levelInfo.title)
}
// output args
for index, item := range args {
format := " %s"
if index == 0 {
format = "%s"
}
output := fmt.Sprint(item)
output = strings.TrimSpace(output)
message += fmt.Sprintf(format, output)
}
// clean up
if flagColorful {
message += colorReset
}
if message == "" {
message = "nothing to log"
}
_, err := fmt.Fprintln(w.writer, message)
if err != nil {
panic(err)
}
}
}