-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathloggers_test.go
More file actions
107 lines (95 loc) · 1.96 KB
/
loggers_test.go
File metadata and controls
107 lines (95 loc) · 1.96 KB
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
package log
import (
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type countMsgs struct {
count int
lInfo *LoggerInfo
}
func (c *countMsgs) Log(lvl int, msg string) {
c.count++
}
func (c *countMsgs) Close() {}
func (c *countMsgs) GetLoggerInfo() *LoggerInfo {
return c.lInfo
}
func TestRegisterLogger(t *testing.T) {
lInfo := &LoggerInfo{
DebugLvl: 3,
UseColors: false,
ShowTime: false,
Padding: false,
}
c := &countMsgs{
count: 0,
lInfo: lInfo,
}
key := RegisterLogger(c)
defer UnregisterLogger(key)
Lvl1("testing")
Lvl3("testing")
Lvl5("testing")
if c.count != 2 {
t.Fatal("wrong count")
}
}
func TestUnregisterLogger(t *testing.T) {
lInfo := &LoggerInfo{
DebugLvl: 3,
UseColors: false,
ShowTime: false,
Padding: false,
}
c := &countMsgs{
count: 0,
lInfo: lInfo,
}
key := RegisterLogger(c)
Lvl1("testing")
UnregisterLogger(key)
Lvl1("testing")
if c.count != 1 {
t.Fatal("wrong count")
}
}
func TestFileLogger(t *testing.T) {
tempFile, err := ioutil.TempFile("", "test_file_logger.txt")
require.Nil(t, err)
path := tempFile.Name()
lInfo := &LoggerInfo{
DebugLvl: 2,
ShowTime: false,
UseColors: false,
Padding: false,
}
fileLogger, err := NewFileLogger(lInfo, path)
require.Nil(t, err)
key := RegisterLogger(fileLogger)
defer func() {
UnregisterLogger(key)
err := os.Remove(path)
require.Nil(t, err)
}()
Lvl1("testing1")
Lvl2("testing2")
out, err := ioutil.ReadFile(path)
require.Nil(t, err)
require.Equal(t, "1 : fake_name.go:0 (log.TestFileLogger) - testing1\n"+
"2 : fake_name.go:0 (log.TestFileLogger) - testing2\n", string(out))
}
func TestStdLoggerZero(t *testing.T) {
GetStdOut()
SetDebugVisible(0)
Info("One Line Only")
str := GetStdOut()
assert.Equal(t, 2, len(strings.Split(str, "\n")), str)
SetDebugVisible(1)
Info("One Line Only")
str = GetStdOut()
assert.Equal(t, 2, len(strings.Split(str, "\n")), str)
}