-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_handler_test.go
75 lines (56 loc) · 1.6 KB
/
file_handler_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
package clog
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFileLogExist(t *testing.T) {
filename := "test1.log"
defer os.Remove(filename)
handler, err := NewFileHandler(filename, "", 0)
require.NoError(t, err)
logger := NewLogger(handler)
logger.Log(LevelInfo, "one", "two", "three")
logger.Logf(LevelInfo, "%d %d %d", 1, 2, 3)
handler.Close()
if _, err := os.Stat(filename); err != nil || os.IsNotExist(err) {
t.Errorf("cannot find log file %s", filename)
}
}
func TestFileCloseLogFile(t *testing.T) {
filename := "test2.log"
handler, err := NewFileHandler(filename, "", 0)
require.NoError(t, err)
defer handler.Close()
defer os.Remove(filename)
logger := NewLogger(handler)
logger.Log(LevelInfo, "one", "two", "three")
// drastically close the file
handler.file.Close()
// the logger should stay silent
logger.Logf(LevelInfo, "%d %d %d", 1, 2, 3)
// but the handler should return an error
err = handler.LogEntry(LogEntry{
Level: LevelDebug,
Values: []interface{}{"test"},
})
assert.Error(t, err)
}
func TestCloseFileHandler(t *testing.T) {
filename := "test3.log"
handler, err := NewFileHandler(filename, "", 0)
require.NoError(t, err)
defer handler.Close()
defer os.Remove(filename)
logger := NewLogger(handler)
logger.Log(LevelInfo, "one", "two", "three")
// close the handler properly
handler.Close()
// but the handler should not return an error as the outpout should have been diverted
err = handler.LogEntry(LogEntry{
Level: LevelDebug,
Values: []interface{}{"test"},
})
assert.NoError(t, err)
}