-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
119 lines (99 loc) · 2.38 KB
/
log_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
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/log source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package log
import (
"os"
"path/filepath"
"testing"
"time"
"aahframework.org/config.v0"
"aahframework.org/test.v0/assert"
)
func TestLogDefault(t *testing.T) {
configStr := `
log {
# default config
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.NotNil(t, logger)
assert.Nil(t, err)
assert.Equal(t, "DEBUG", logger.Level())
logger.Print("This is print")
logger.Printf("This is print - %s", "yes")
logger.Println("This is print", "yes")
testPanic(logger, "panic", "this is panic")
testPanic(logger, "panicf", "this is panicf")
testPanic(logger, "panicln", "this is panicln")
}
func TestMisc(t *testing.T) {
stats := receiverStats{
lines: 200,
bytes: 764736,
}
assert.Equal(t, int64(764736), stats.Bytes())
assert.Equal(t, int64(200), stats.Lines())
configStr := `
log {
# default config
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.NotNil(t, logger)
assert.Nil(t, err)
assert.Equal(t, "DEBUG", logger.Level())
err = logger.SetReceiver(nil)
assert.Equal(t, "log: receiver is nil", err.Error())
err = logger.SetLevel("MYLEVEL")
assert.Equal(t, "log: unknown log level 'MYLEVEL'", err.Error())
logger, err = New(nil)
assert.Nil(t, logger)
assert.NotNil(t, err)
assert.Equal(t, "log: config is nil", err.Error())
// util
assert.Nil(t, getReceiverByName("SMTP"))
assert.Equal(t, "", formatTime(time.Time{}))
stdLogger := ToGoLogger()
assert.NotNil(t, stdLogger)
assert.NotNil(t, Writer())
stdLogger.Print("This is aah logger binds go logger")
}
func testPanic(logger *Logger, method, msg string) {
defer func() {
if r := recover(); r != nil {
_ = r
}
}()
if method == "panic" {
logger.Panic(msg)
} else if method == "panicf" {
logger.Panicf("%s", msg)
} else if method == "panicln" {
logger.Panicln(msg)
}
}
func getPwd() string {
pwd, _ := os.Getwd()
return pwd
}
func cleaupFiles(match string) {
pwd := getPwd()
dir, err := os.Open(pwd)
if err != nil {
return
}
infos, err := dir.Readdir(-1)
if err != nil {
return
}
for _, info := range infos {
if !info.IsDir() {
if found, _ := filepath.Match(match, info.Name()); found {
_ = os.Remove(filepath.Join(pwd, info.Name()))
}
}
}
}