-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler_test.go
executable file
·110 lines (96 loc) · 2.83 KB
/
filehandler_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
package verbose
import (
"os"
"path/filepath"
"testing"
)
const (
testLogFile string = "test.log"
testLogDir string = "logs"
)
func cleanup() {
os.Remove(testLogFile)
os.RemoveAll(testLogDir)
}
func TestDefaults(t *testing.T) {
defer cleanup()
fh, err := NewFileHandler(testLogFile)
if err != nil {
t.Fatalf("Error making file handler: %s", err.Error())
}
if fh.min != LogLevelDebug {
t.Errorf("Incorrect default minimum. Expected %d, got %d", LogLevelDebug, fh.min)
}
if fh.max != LogLevelFatal {
t.Errorf("Incorrect default maximum. Expected %d, got %d", LogLevelFatal, fh.max)
}
if fh.separate {
t.Error("Incorrect separate field. Expected false, got true")
}
fh, err = NewFileHandler(testLogDir)
if err != nil {
t.Fatalf("Error making file handler: %s", err.Error())
}
if !fh.separate {
t.Error("Incorrect separate field. Expected true, got false")
}
}
func TestFileHandlerLevelSetting(t *testing.T) {
fh := &FileHandler{}
fh.SetLevel(LogLevelWarning)
if fh.min != LogLevelWarning {
t.Errorf("Min level not set correctly. Expected %d, got %d", LogLevelWarning, fh.min)
}
if fh.max != LogLevelWarning {
t.Errorf("Max level not set correctly. Expected %d, got %d", LogLevelWarning, fh.max)
}
fh.SetMinLevel(LogLevelInfo)
if fh.min != LogLevelInfo {
t.Errorf("Min level not set correctly. Expected %d, got %d", LogLevelInfo, fh.min)
}
fh.SetMaxLevel(LogLevelAlert)
if fh.max != LogLevelAlert {
t.Errorf("Max level not set correctly. Expected %d, got %d", LogLevelAlert, fh.max)
}
if fh.Handles(LogLevelDebug) {
t.Errorf("Incorrect Handles result. Expected false, got %t", fh.Handles(LogLevelDebug))
}
if fh.Handles(LogLevelEmergency) {
t.Errorf("Incorrect Handles result. Expected false, got %t", fh.Handles(LogLevelEmergency))
}
if !fh.Handles(LogLevelCritical) {
t.Errorf("Incorrect Handles result. Expected true, got %t", fh.Handles(LogLevelCritical))
}
}
func TestFileHandlerWriteLog(t *testing.T) {
defer cleanup()
// Test write to single file
fh, err := NewFileHandler(testLogFile)
if err != nil {
t.Fatalf("Error making file handler: %s", err.Error())
}
e := NewEntry(&Logger{name: "logger"})
e.Level = LogLevelAlert
e.Message = "What? No coffee!?"
fh.WriteLog(e)
stat, _ := os.Stat(testLogFile)
if stat.Size() != 55 {
t.Errorf("Incorrect log file size. Expected 55, got %d", stat.Size())
}
// Test write to directory
fh, err = NewFileHandler(testLogDir)
if err != nil {
t.Fatalf("Error making file handler: %s", err.Error())
}
e = NewEntry(&Logger{name: "logger"})
e.Level = LogLevelAlert
e.Message = "What? No coffee!?"
fh.WriteLog(e)
stat, err = os.Stat(filepath.Join(testLogDir, "alert-logger.log"))
if err != nil {
t.Fatalf("Error stating log file: %s", err.Error())
}
if stat.Size() != 55 {
t.Errorf("Incorrect log file size. Expected 55, got %d", stat.Size())
}
}