-
Notifications
You must be signed in to change notification settings - Fork 16
/
runtime.go
65 lines (55 loc) · 965 Bytes
/
runtime.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
package logger
import (
"os"
)
var (
runtime *Runtime
muted = &OutputSettings{}
verbose = &OutputSettings{
Info: true,
Timer: true,
Error: true,
}
)
func init() {
runtime = &Runtime{
Writers: []OutputWriter{
NewStandardOutput(os.Stderr),
},
}
}
type OutputWriter interface {
Init()
Write(log *Log)
}
type OutputSettings struct {
Info bool
Timer bool
Error bool
}
type Runtime struct {
Writers []OutputWriter
}
func (runtime *Runtime) Log(log *Log) {
if len(runtime.Writers) == 0 {
return
}
// Avoid getting into a loop if there is just one writer
if len(runtime.Writers) == 1 {
runtime.Writers[0].Write(log)
} else {
for _, w := range runtime.Writers {
w.Write(log)
}
}
}
// Add a new writer
func Hook(writer OutputWriter) {
writer.Init()
runtime.Writers = append(runtime.Writers, writer)
}
// Legacy method
func SetOutput(file *os.File) {
writer := NewStandardOutput(file)
runtime.Writers[0] = writer
}