-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
53 lines (41 loc) · 1000 Bytes
/
output_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
package log
import (
"bufio"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDailyFileOutput(t *testing.T) {
file, err := DailyFileOutput("test_daily_file", "./")
if err != nil {
t.Fatal(err)
}
filename := file.Name()
defer func() {
file.Close()
os.Remove(file.Name())
}()
logger := New()
logger.SetOutput(io.MultiWriter(os.Stdout, file))
logger.Info("Test info log written to file")
logger.Error("Test error log written to file")
file.Close()
_, err = os.Stat(file.Name())
assert.Nil(t, err)
file, err = os.Open(filename)
if err != nil {
t.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
assert.Equal(t, 2, len(lines))
assert.True(t, strings.HasSuffix(lines[0], "INFO Test info log written to file"))
assert.True(t, strings.HasSuffix(lines[1], "ERROR Test error log written to file"))
}