-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
147 lines (123 loc) · 3.07 KB
/
example_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package rotatelogs_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"testing"
"time"
rotatelogs "github.com/iproj/file-rotatelogs"
"github.com/stretchr/testify/assert"
)
func ExampleForceNewFile() {
logDir, err := ioutil.TempDir("", "rotatelogs_test")
if err != nil {
fmt.Println("could not create log directory ", err)
return
}
logPath := fmt.Sprintf("%s/test.log", logDir)
for i := 0; i < 2; i++ {
writer, err := rotatelogs.New(logPath,
rotatelogs.ForceNewFile(),
)
if err != nil {
fmt.Println("Could not open log file ", err)
return
}
n, err := writer.Write([]byte("test"))
if err != nil || n != 4 {
fmt.Println("Write failed ", err, " number written ", n)
return
}
err = writer.Close()
if err != nil {
fmt.Println("Close failed ", err)
return
}
}
files, err := ioutil.ReadDir(logDir)
if err != nil {
fmt.Println("ReadDir failed ", err)
return
}
for _, file := range files {
fmt.Println(file.Name(), file.Size())
}
err = os.RemoveAll(logDir)
if err != nil {
fmt.Println("RemoveAll failed ", err)
return
}
// OUTPUT:
// test.log 4
// test.log.1 4
}
func TestTooMuchLog(t *testing.T) {
var (
testDir = "test_much_log"
testLogPath = filepath.Join(testDir, "access_log")
rotationCount = 3
N = 12 // N > 10
)
err := os.Mkdir(testDir, 0777)
assert.Nil(t, err)
defer os.RemoveAll(testDir)
assert.Nil(t, err)
rl, err := rotatelogs.New(
testLogPath+".%Y%m%d%H%M",
rotatelogs.WithLinkName(testLogPath),
rotatelogs.WithRotationCount(uint(rotationCount)),
rotatelogs.WithRotationSize(12), // Log contentSize > 12
)
assert.Nil(t, err)
log.SetOutput(rl)
for i := 0; i < N; i++ {
log.Printf("Test content %d\n", i)
}
files, _ := ioutil.ReadDir(testDir)
assert.Equal(t, rotationCount+1, len(files))
bytez, err := ioutil.ReadFile(testLogPath)
assert.Nil(t, err)
assert.Equal(t, strconv.Itoa(N-1), string(bytez[len(bytez)-3:len(bytez)-1]))
}
func TestSizePriorityOverTime(t *testing.T) {
var (
testDir = "test_size_priority_over_time"
testLogPath = filepath.Join(testDir, "access_log")
rotationCount = 2
N = 12
)
err := os.Mkdir(testDir, 0777)
assert.Nil(t, err)
defer os.RemoveAll(testDir)
assert.Nil(t, err)
rl, err := rotatelogs.New(
testLogPath+".%Y%m%d%H%M%S", // Accurate to seconds
rotatelogs.WithRotationCount(uint(rotationCount)),
rotatelogs.WithRotationSize(12000),
)
assert.Nil(t, err)
log.SetOutput(rl)
for i := 0; i < N; i++ {
log.Printf("Test content %d\n", i)
// N * sleepTime > 1s
time.Sleep(120 * time.Millisecond)
}
files, _ := ioutil.ReadDir(testDir)
assert.Equal(t, 1, len(files)) // N * contentSize < rotationSize
rl, err = rotatelogs.New(
testLogPath+".%Y%m%d%H%M%S", // Accurate to seconds
rotatelogs.WithRotationCount(uint(rotationCount)),
)
assert.Nil(t, err)
log.SetOutput(rl)
for i := 0; i < N; i++ {
log.Printf("Test content %d\n", i)
// N * sleepTime > 1s
time.Sleep(120 * time.Millisecond)
}
files, _ = ioutil.ReadDir(testDir)
assert.Equal(t, rotationCount, len(files))
}