This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
log_test.go
108 lines (90 loc) · 2.44 KB
/
log_test.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
package holochain
import (
"bytes"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
// needed to setup the holochain environment, not really a test.
func TestNewLog(t *testing.T) {
Convey("it should log according format string", t, func() {
var buf bytes.Buffer
l1 := Logger{Enabled: true}
err := l1.New(&buf)
So(err, ShouldBeNil)
l2 := Logger{
Enabled: true,
Format: "L2:%{message}",
}
err = l2.New(&buf)
So(err, ShouldBeNil)
l1.Log("fish")
l2.Logf("%d blue", 2)
So(buf.String(), ShouldEqual, "fish\nL2:2 blue\n")
})
Convey("it should handle time", t, func() {
var buf bytes.Buffer
l := Logger{
Enabled: true,
Format: "%{time}:%{message}",
}
tf, f := l.setupTime("X:%{message}")
So(tf, ShouldEqual, "")
So(f, ShouldEqual, "X:%{message}")
tf, f = l.setupTime("x%{time}y")
So(f, ShouldEqual, "x%{time}y")
So(tf, ShouldEqual, "Jan _2 15:04:05")
tf, f = l.setupTime("x%{time:xxy}y")
So(f, ShouldEqual, "x%{time}y")
So(tf, ShouldEqual, "xxy")
l.New(&buf)
now := time.Unix(1, 1)
So(l._parse("fish", &now), ShouldEqual, now.Format(time.Stamp)+":fish")
})
Convey("it should handle color", t, func() {
var buf bytes.Buffer
l := Logger{
Enabled: true,
Format: "%{color:blue}%{time}:%{message}",
}
c, f := l.setupColor("x")
So(c, ShouldBeNil)
So(f, ShouldEqual, "x")
c, f = l.setupColor("prefix%{color:red}%{message}")
So(fmt.Sprintf("%v", c), ShouldEqual, "&{[31] <nil>}")
So(f, ShouldEqual, "prefix%{message}")
l.New(&buf)
now := time.Unix(1, 1)
So(l._parse("fish", &now), ShouldEqual, now.Format(time.Stamp)+":fish")
})
Convey("it should log with a prefix", t, func() {
var buf bytes.Buffer
l := Logger{
Enabled: true,
}
l.New(&buf)
l.Log("onefish")
l.SetPrefix("[PREFIX]")
l.Log("twofish")
l.SetPrefix("[COLOR PREFIX]")
l.Log("threefish")
So(buf.String(), ShouldEqual, "onefish\n[PREFIX]twofish\n[COLOR PREFIX]threefish\n")
})
Convey("it should handle file name and line number", t, func() {
var buf bytes.Buffer
l := Logger{
Enabled: true,
Format: "%{file}.%{line}:%{message}",
}
l.New(&buf)
doDebug(l, "fish")
So(buf.String(), ShouldEqual, "log_test.go.97:fish\n")
})
}
// we do this because in the case of file & line needs to know how many
// calls back up the stack to use for calculating the line number and we
// allways have a wrapper function around the log
func doDebug(l Logger, m string) {
l.Log(m)
}