-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_test.go
68 lines (56 loc) · 1.55 KB
/
hook_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
// 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 (
"fmt"
"os"
"testing"
"time"
"aahframework.org/config.v0"
"aahframework.org/test.v0/assert"
)
func TestLogAddHook(t *testing.T) {
err := AddHook("hook1", func(e Entry) {
assert.NotNil(t, e)
})
assert.Nil(t, err)
err = AddHook("hook2", func(e Entry) {
assert.NotNil(t, e)
})
assert.Nil(t, err)
// Already added
err = AddHook("hook1", func(e Entry) {
assert.NotNil(t, e)
})
assert.Equal(t, "log: hook name 'hook1' is already added, skip it", err.Error())
// Nil hook
err = AddHook("nilhook", nil)
assert.Equal(t, ErrHookFuncIsNil, err)
}
func TestLogHook(t *testing.T) {
configStr := `
log {
receiver = "console"
level = "debug"
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %line %custom:- %message"
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.FailNowOnError(t, err, "unexpected error")
// Add hook
_ = logger.AddHook("hook1", func(e Entry) {
assert.NotNil(t, e)
fmt.Println(e)
})
logger.Trace("I shoudn't see this msg, because standard logger level is DEBUG")
logger.Debug("I would like to see this message, debug is useful for dev")
logger.Info("Yes, I would love to see")
logger.Warn("Yes, yes it's an warning")
logger.Error("Yes, yes, yes - finally an error")
exit = func(code int) {}
logger.Fatal("Yes, yes, yes - at last fatal")
exit = os.Exit
time.Sleep(1 * time.Millisecond)
}