-
Notifications
You must be signed in to change notification settings - Fork 31
/
blog4go_test.go
116 lines (99 loc) · 2.29 KB
/
blog4go_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
// Copyright (c) 2015, huangjunwei <huangjunwei@youmi.net>. All rights reserved.
package blog4go
import (
"fmt"
"os/exec"
"testing"
"time"
)
func TestGlobalOperation(t *testing.T) {
err := NewFileWriter("/tmp", false)
defer func() {
Close()
// clean logs
_, err = exec.Command("/bin/sh", "-c", "/bin/rm /tmp/*.log*").Output()
if nil != err {
t.Errorf("clean files failed. err: %s", err.Error())
}
}()
// test file writer hook
hook := NewMyHook()
SetHook(hook)
SetHookLevel(INFO)
fmt.Println("something")
Debug("something")
fmt.Println("something2")
Debugf("%s", "something")
// wait for hook called
time.Sleep(1 * time.Millisecond)
if 0 != hook.cnt {
t.Error("hook called not valid")
}
if DEBUG == hook.Level() || "something" == hook.Message() {
t.Errorf("hook parameters wrong. level: %s, message: %s", hook.level.String(), hook.Message())
}
// async
Info("yes")
// wait for hook called
time.Sleep(1 * time.Millisecond)
if 1 != hook.Cnt() {
t.Error("hook not called")
}
if INFO != hook.Level() || "yes" != hook.Message() {
t.Errorf("hook parameters wrong. level: %d, message: %s", hook.level, hook.Message())
}
// sync
SetHookAsync(false)
Warn("warn")
if 2 != hook.Cnt() {
t.Error("hook not called")
}
if WARNING != hook.Level() || "warn" != hook.Message() {
t.Errorf("hook parameters wrong. level: %d, message: %s", hook.level, hook.Message())
}
// test basic operations
SetTags(map[string]string{"tagName": "tagValue"})
Tags()
Debug("Debug", 1)
Debugf("%s", "Debug")
Trace("Trace", 2)
Tracef("%s", "Trace")
Info("Info", 3)
Infof("%s", "Info")
Warn("Warn", 4)
Warnf("%s", "Warn")
Error("Error", 5)
Errorf("%s", "Error")
Critical("Critical", 6)
Criticalf("%s", "Critical")
Flush()
SetHookAsync(true)
Colored()
SetColored(true)
TimeRotated()
SetTimeRotated(true)
Level()
SetLevel(CRITICAL)
Retentions()
SetRetentions(0)
SetRetentions(7)
RotateLines()
SetRotateLines(0)
SetRotateLines(100000)
RotateSize()
SetRotateSize(0)
SetRotateSize(1024 * 1024 * 500)
Debug("Debug", 1)
Debugf("%s\\", "Debug")
Trace("Trace", 2)
Tracef("%s", "Trace")
Info("Info", 3)
Infof("%s", "Info")
Warn("Warn", 4)
Warnf("%s", "Warn")
Error("Error", 5)
Errorf("%s", "Error")
Critical("Critical", 6)
Criticalf("%s", "Critical")
SetBufferSize(0)
}