-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_receiver_test.go
137 lines (118 loc) · 3.65 KB
/
console_receiver_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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 (
"io/ioutil"
"os"
"testing"
"aahframework.org/config.v0"
"aahframework.org/test.v0/assert"
)
func TestConsoleLoggerTextJSON(t *testing.T) {
// Text 1
textConfigStr1 := `
log {
receiver = "console"
level = "debug"
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %longfile %line %custom:- %message"
color = true
}
`
testConsoleLogger(t, textConfigStr1)
// Text 2
textConfigStr2 := `
log {
receiver = "console"
level = "debug"
pattern = "%time:2006-01-02 15:04:05.000 %appname %insname %reqid %principal %level:-5 %shortfile %line %custom:- %message"
}
`
testConsoleLogger(t, textConfigStr2)
// JSON
jsonConfigStr := `
log {
receiver = "console"
level = "debug"
format = "json"
}
`
testConsoleLogger(t, jsonConfigStr)
}
func TestConsoleLoggerUnsupportedFormat(t *testing.T) {
configStr := `
log {
# default config plus
pattern = "%utctime:2006-01-02 15:04:05.000 %level:-5 %longfile %line %custom:- %message"
format = "xml"
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.Nil(t, logger)
assert.Equal(t, "log: unsupported format 'xml'", err.Error())
}
func TestConsoleLoggerUnknownFormatFlag(t *testing.T) {
configStr := `
log {
# default config plus
pattern = "%time:2006-01-02 15:04:05.000 %level:-5 %myfile %line %custom:- %message"
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.Nil(t, logger)
assert.Equal(t, "fmtflag: unknown flag 'myfile'", err.Error())
}
func TestConsoleLoggerUnknownLevel(t *testing.T) {
configStr := `
log {
# default config plus
level = "MYLEVEL"
pattern = "%time:2006-01-02 15:04:05.000 %level:-5 %message"
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.Nil(t, logger)
assert.Equal(t, "log: unknown log level 'MYLEVEL'", err.Error())
}
func TestConsoleLoggerDefaults(t *testing.T) {
configStr := `
log {
# default config
}
`
cfg, _ := config.ParseString(configStr)
logger, err := New(cfg)
assert.NotNil(t, logger)
assert.Nil(t, err)
logger.SetWriter(ioutil.Discard)
// receiver nil scenario
logger.receiver = nil
err = logger.SetPattern("%time:2006-01-02 15:04:05.000 %level:-5 %message")
assert.Equal(t, "log: receiver is nil", err.Error())
}
func testConsoleLogger(t *testing.T, cfgStr string) {
cfg, _ := config.ParseString(cfgStr)
logger, err := New(cfg)
assert.FailNowOnError(t, err, "unexpected error")
logger.Trace("I shoudn't see this msg, because standard logger level is DEBUG")
logger.Tracef("I shoudn't see this msg, because standard logger level is DEBUG: %v", 4)
logger.WithField("appname", "testlogapp").WithField("insname", "app-sfo-cn-01").
Debug("I would like to see this message, debug is useful for dev")
logger.Debugf("I would like to see this message, debug is useful for %v", "dev")
logger.WithField("reqid", "40139CA6368607085BF6").WithField("insname", "app-sfo-cn-01").
Info("Yes, I would love to see")
logger.Infof("Yes, I would love to %v", "see")
logger.WithField("principal", "jeevanandam").Warn("Yes, yes it's an warning")
logger.Warnf("Yes, yes it's an %v", "warning")
logger.Error("Yes, yes, yes - finally an error")
logger.Errorf("Yes, yes, yes - %v", "finally an error")
exit = func(code int) {}
logger.Fatal("Yes, yes, yes - at last fatal")
logger.Fatalf("Yes, yes, yes - %v", "at last fatal")
logger.Fatalln("Yes, yes, yes ", "at last fatal")
exit = os.Exit
assert.NotNil(t, logger.ToGoLogger())
}