-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_file_test.go
54 lines (50 loc) · 1.36 KB
/
handler_file_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
package logging
import (
"github.com/hhkbp2/testify/require"
"io/ioutil"
"os"
"testing"
)
var (
testFileName = "./test.log"
testFileMode = os.O_TRUNC
testBufferSize = 0
)
func TestFileHandler(t *testing.T) {
handler, err := NewFileHandler(testFileName, testFileMode, testBufferSize)
require.Nil(t, err)
logger := GetLogger("file1")
logger.AddHandler(handler)
message := "test"
logger.Errorf(message)
logger.RemoveHandler(handler)
handler.Close()
// open the log file and check its content equals to message
// then clean it up.
content, err := ioutil.ReadFile(testFileName)
require.Nil(t, err)
require.Equal(t, message+"\n", string(content))
err = os.Remove(testFileName)
require.Nil(t, err)
}
func TestFileHandler_Asctime(t *testing.T) {
handler, err := NewFileHandler(testFileName, testFileMode, testBufferSize)
formatter := NewStandardFormatter(
"%(asctime)s %(message)s",
"%Y-%m-%d %H:%M:%S %3n")
handler.SetFormatter(formatter)
require.Nil(t, err)
logger := GetLogger("file2")
logger.AddHandler(handler)
message := "test"
logger.Errorf(message)
logger.RemoveHandler(handler)
handler.Close()
// open the log file and check its content equals to message
// then clean it up.
content, err := ioutil.ReadFile(testFileName)
require.Nil(t, err)
require.Equal(t, 24+len(message)+1, len(content))
err = os.Remove(testFileName)
require.Nil(t, err)
}