-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_rotating_file_test.go
119 lines (112 loc) · 2.96 KB
/
handler_rotating_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
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
109
110
111
112
113
114
115
116
117
118
119
package logging
import (
"github.com/hhkbp2/testify/require"
"os"
"strings"
"testing"
"time"
)
var (
testBufferFlushTime = time.Millisecond * 5000
testInputChanSize = 1
testRotateMaxBytes = uint64(5 * 1000) // 5k bytes
testRotateBackupCount = uint32(1)
)
func checkFileSize(t *testing.T, filename string) {
fileInfo, err := os.Stat(filename)
require.Nil(t, err)
require.True(t, fileInfo.Size() > 0)
require.True(t, uint64(fileInfo.Size()) <= testRotateMaxBytes)
}
func removeFile(t *testing.T, filename string) {
err := os.Remove(filename)
require.Nil(t, err)
}
func TestRotatingFileHandler_TruncateWithBackup(t *testing.T) {
defer Shutdown()
handler, err := NewRotatingFileHandler(
testFileName,
testFileMode,
testBufferSize,
testBufferFlushTime,
testInputChanSize,
testRotateMaxBytes,
testRotateBackupCount)
require.Nil(t, err)
logger := GetLogger("rfile1")
logger.AddHandler(handler)
// every message is 99 byte, and \n
message := strings.Repeat("abcdefghij", 9) + "rstuvwxyz"
size := uint64(len(message) + 1)
total := testRotateMaxBytes * (uint64(testRotateBackupCount) + 2) / size
for i := uint64(0); i < total; i++ {
logger.Errorf(message)
}
logger.RemoveHandler(handler)
handler.Close()
checkFileSize(t, testFileName)
checkFileSize(t, testFileName+".1")
removeFile(t, testFileName)
removeFile(t, testFileName+".1")
}
func TestRotatingFileHandler_AppendWithoutBackup(t *testing.T) {
defer Shutdown()
// clean up the existing log file
if FileExists(testFileName) {
require.Nil(t, os.Remove(testFileName))
}
backupCount := uint32(0)
handler, err := NewRotatingFileHandler(
testFileName,
os.O_APPEND,
testBufferSize,
testBufferFlushTime,
testInputChanSize,
testRotateMaxBytes,
backupCount)
require.Nil(t, err)
logger := GetLogger("rfile2")
logger.AddHandler(handler)
message := strings.Repeat("abcdefghij", 9) + "rstuvwxyz"
size := uint64(len(message) + 1)
totalSize := testRotateMaxBytes * (uint64(testRotateBackupCount) + 2)
times := totalSize / size
require.Equal(t, totalSize, size*times)
for i := uint64(0); i < times; i++ {
logger.Errorf(message)
}
logger.RemoveHandler(handler)
handler.Close()
fileInfo, err := os.Stat(testFileName)
require.Nil(t, err)
require.True(t, uint64(fileInfo.Size()) > testRotateMaxBytes)
require.Equal(t, totalSize, uint64(fileInfo.Size()))
removeFile(t, testFileName)
}
func BenchmarkRotatingFileHandler(b *testing.B) {
b.StopTimer()
defer Shutdown()
if FileExists(testFileName) {
os.Remove(testFileName)
}
rotateMaxBytes := uint64(1024 * 1024 * 1024 * 1) // 1G
backupCount := uint32(0)
handler, err := NewRotatingFileHandler(
testFileName,
os.O_APPEND,
testBufferSize,
testBufferFlushTime,
testInputChanSize,
rotateMaxBytes,
backupCount)
if err != nil {
panic("fail to get handler")
}
logger := GetLogger("rfileBen")
logger.AddHandler(handler)
message := strings.Repeat("abcdefghij", 10)
b.StartTimer()
for i := 0; i < b.N; i++ {
logger.Errorf(message)
}
}