1 // Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
8 "github.com/revel/config"
9 "github.com/revel/revel/logger"
10 "github.com/stretchr/testify/assert"
17 // A counter for the tester
19 debug, info, warn, error, critical int
29 debug, info, warn, error, critical int
34 var singleCases = []testData{
35 {config: []string{"log.crit.output"},
36 result: testResult{0, 0, 0, 0, 1}},
37 {config: []string{"log.error.output"},
38 result: testResult{0, 0, 0, 1, 1}},
39 {config: []string{"log.warn.output"},
40 result: testResult{0, 0, 1, 0, 0}},
41 {config: []string{"log.info.output"},
42 result: testResult{0, 1, 0, 0, 0}},
43 {config: []string{"log.debug.output"},
44 result: testResult{1, 0, 0, 0, 0}},
48 func TestSingleCases(t *testing.T) {
49 rootLog := logger.New()
50 for _, testCase := range singleCases {
51 testCase.logTest(rootLog, t)
57 var filterCases = []testData{
58 {config: []string{"log.crit.filter.module.app"},
59 result: testResult{0, 0, 0, 0, 1}},
60 {config: []string{"log.crit.filter.module.appa"},
61 result: testResult{0, 0, 0, 0, 0}},
62 {config: []string{"log.error.filter.module.app"},
63 result: testResult{0, 0, 0, 1, 1}},
64 {config: []string{"log.error.filter.module.appa"},
65 result: testResult{0, 0, 0, 0, 0}},
66 {config: []string{"log.warn.filter.module.app"},
67 result: testResult{0, 0, 1, 0, 0}},
68 {config: []string{"log.warn.filter.module.appa"},
69 result: testResult{0, 0, 0, 0, 0}},
70 {config: []string{"log.info.filter.module.app"},
71 result: testResult{0, 1, 0, 0, 0}},
72 {config: []string{"log.info.filter.module.appa"},
73 result: testResult{0, 0, 0, 0, 0}},
74 {config: []string{"log.debug.filter.module.app"},
75 result: testResult{1, 0, 0, 0, 0}},
76 {config: []string{"log.debug.filter.module.appa"},
77 result: testResult{0, 0, 0, 0, 0}},
81 func TestFilterCases(t *testing.T) {
82 rootLog := logger.New("module", "app")
83 for _, testCase := range filterCases {
84 testCase.logTest(rootLog, t)
90 var nfilterCases = []testData{
91 {config: []string{"log.crit.nfilter.module.appa"},
92 result: testResult{0, 0, 0, 0, 1}},
93 {config: []string{"log.crit.nfilter.modules.appa"},
94 result: testResult{0, 0, 0, 0, 0}},
95 {config: []string{"log.crit.nfilter.module.app"},
96 result: testResult{0, 0, 0, 0, 0}},
97 {config: []string{"log.error.nfilter.module.appa"}, // Special case, when error is not nill critical inherits from error
98 result: testResult{0, 0, 0, 1, 1}},
99 {config: []string{"log.error.nfilter.module.app"},
100 result: testResult{0, 0, 0, 0, 0}},
101 {config: []string{"log.warn.nfilter.module.appa"},
102 result: testResult{0, 0, 1, 0, 0}},
103 {config: []string{"log.warn.nfilter.module.app"},
104 result: testResult{0, 0, 0, 0, 0}},
105 {config: []string{"log.info.nfilter.module.appa"},
106 result: testResult{0, 1, 0, 0, 0}},
107 {config: []string{"log.info.nfilter.module.app"},
108 result: testResult{0, 0, 0, 0, 0}},
109 {config: []string{"log.debug.nfilter.module.appa"},
110 result: testResult{1, 0, 0, 0, 0}},
111 {config: []string{"log.debug.nfilter.module.app"},
112 result: testResult{0, 0, 0, 0, 0}},
116 func TestNotFilterCases(t *testing.T) {
117 rootLog := logger.New("module", "app")
118 for _, testCase := range nfilterCases {
119 testCase.logTest(rootLog, t)
125 var offCases = []testData{
126 {config: []string{"log.all.output", "log.error.output=off"},
127 result: testResult{1, 1, 1, 0, 1}},
131 func TestOffCases(t *testing.T) {
132 rootLog := logger.New("module", "app")
133 for _, testCase := range offCases {
134 testCase.logTest(rootLog, t)
139 // Duplicate test cases
140 var duplicateCases = []testData{
141 {config: []string{"log.all.output", "log.error.output", "log.error.filter.module.app"},
142 result: testResult{1, 1, 1, 2, 1}},
145 // test duplicate cases
146 func TestDuplicateCases(t *testing.T) {
147 rootLog := logger.New("module", "app")
148 for _, testCase := range duplicateCases {
149 testCase.logTest(rootLog, t)
154 // Contradicting cases
155 var contradictCases = []testData{
156 {config: []string{"log.all.output", "log.error.output=off", "log.all.output"},
157 result: testResult{1, 1, 1, 0, 1}},
158 {config: []string{"log.all.output", "log.error.output=off", "log.debug.filter.module.app"},
159 result: testResult{2, 1, 1, 0, 1}},
160 {config: []string{"log.all.filter.module.app", "log.info.output=off", "log.info.filter.module.app"},
161 result: testResult{1, 2, 1, 1, 1}},
162 {config: []string{"log.all.output", "log.info.output=off", "log.info.filter.module.app"},
163 result: testResult{1, 1, 1, 1, 1}},
166 // Contradiction test
167 func TestContradictCases(t *testing.T) {
168 rootLog := logger.New("module", "app")
169 for _, testCase := range contradictCases {
170 testCase.logTest(rootLog, t)
176 var allCases = []testData{
177 {config: []string{"log.all.filter.module.app"},
178 result: testResult{1, 1, 1, 1, 1}},
179 {config: []string{"log.all.output"},
180 result: testResult{2, 2, 2, 2, 2}},
184 func TestAllCases(t *testing.T) {
185 rootLog := logger.New("module", "app")
186 for i, testCase := range allCases {
187 testCase.logTest(rootLog, t)
188 allCases[i] = testCase
190 rootLog = logger.New()
191 for i, testCase := range allCases {
192 testCase.logTest(rootLog, t)
193 allCases[i] = testCase
195 for _, testCase := range allCases {
201 func (c *testCounter) Log(r *logger.Record) error {
203 case logger.LvlDebug:
209 case logger.LvlError:
214 panic("Unknown log level")
218 func (td *testData) logTest(rootLog logger.MultiLogger, t *testing.T) {
220 td.tc = &testCounter{}
223 newContext := config.NewContext()
224 for _, i := range td.config {
225 iout := strings.Split(i, "=")
227 newContext.SetOption(iout[0], iout[1])
229 newContext.SetOption(i, "test")
233 newContext.SetOption("specialUseFlag", "true")
235 handler := logger.InitializeFromConfig("test", newContext)
237 rootLog.SetHandler(handler)
239 td.runLogTest(rootLog)
242 func (td *testData) runLogTest(log logger.MultiLogger) {
250 func (td *testData) validate(t *testing.T) {
251 t.Logf("Test %#v expected %#v", td.tc, td.result)
252 assert.Equal(t, td.result.debug, td.tc.debug, "Debug failed "+strings.Join(td.config, " "))
253 assert.Equal(t, td.result.info, td.tc.info, "Info failed "+strings.Join(td.config, " "))
254 assert.Equal(t, td.result.warn, td.tc.warn, "Warn failed "+strings.Join(td.config, " "))
255 assert.Equal(t, td.result.error, td.tc.error, "Error failed "+strings.Join(td.config, " "))
256 assert.Equal(t, td.result.critical, td.tc.critical, "Critical failed "+strings.Join(td.config, " "))
259 // Add test to the function map
260 func counterInit(tc *testCounter) {
261 logger.LogFunctionMap["test"] = func(c *logger.CompositeMultiHandler, logOptions *logger.LogOptions) {
262 // Output to the test log and the stdout
263 outHandler := logger.LogHandler(
264 logger.NewListLogHandler(tc,
265 logger.StreamHandler(os.Stdout, logger.TerminalFormatHandler(false, true))),
267 if logOptions.HandlerWrap != nil {
268 outHandler = logOptions.HandlerWrap.SetChild(outHandler)
271 c.SetHandlers(outHandler, logOptions)