// Copyright (c) 2012 - Cloud Instruments Co., Ltd. // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package seelog import ( "fmt" "path/filepath" "regexp" "strings" "testing" ) type customTestReceiverOutput struct { initCalled bool dataPassed string messageOutput string levelOutput LogLevel closed bool flushed bool } type customTestReceiver struct{ co *customTestReceiverOutput } func (cr *customTestReceiver) ReceiveMessage(message string, level LogLevel, context LogContextInterface) error { cr.co.messageOutput = message cr.co.levelOutput = level return nil } func (cr *customTestReceiver) String() string { return fmt.Sprintf("custom data='%s'", cr.co.dataPassed) } func (cr *customTestReceiver) AfterParse(initArgs CustomReceiverInitArgs) error { cr.co = new(customTestReceiverOutput) cr.co.initCalled = true cr.co.dataPassed = initArgs.XmlCustomAttrs["test"] return nil } func (cr *customTestReceiver) Flush() { cr.co.flushed = true } func (cr *customTestReceiver) Close() error { cr.co.closed = true return nil } var re = regexp.MustCompile(`[^a-zA-Z0-9]+`) func getTestFileName(testName, postfix string) string { if len(postfix) != 0 { return strings.ToLower(re.ReplaceAllString(testName, "_")) + "_" + postfix + "_test.log" } return strings.ToLower(re.ReplaceAllString(testName, "_")) + "_test.log" } var parserTests []parserTest type parserTest struct { testName string config string expected *logConfig //interface{} errorExpected bool parserConfig *CfgParseParams } func getParserTests() []parserTest { if parserTests == nil { parserTests = make([]parserTest, 0) testName := "Simple file output" testLogFileName := getTestFileName(testName, "") testConfig := ` ` testExpected := new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter, _ := newFileWriter(testLogFileName) testHeadSplitter, _ := newSplitDispatcher(defaultformatter, []interface{}{testfileWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Filter dispatcher" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter, _ = newFileWriter(testLogFileName) testFilter, _ := newFilterDispatcher(defaultformatter, []interface{}{testfileWriter}, DebugLvl, InfoLvl, CriticalLvl) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testFilter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Console writer" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ := newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "SMTP writer" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testSMTPWriter := newSMTPWriter( "sa", "sn", []string{"ra1", "ra2", "ra3"}, "hn", "123", "un", "up", []string{"cacdp1", "cacdp2"}, DefaultSubjectPhrase, nil, ) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testSMTPWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "SMTP writer custom header and subject configuration" testConfig = `
` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testSMTPWriter = newSMTPWriter( "sa", "sn", []string{"ra1"}, "hn", "123", "un", "up", []string{"cacdp1"}, "ohlala", []string{"Priority: Urgent", "Importance: high", "Sensitivity: Company-Confidential", "Auto-Submitted: auto-generated"}, ) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testSMTPWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Default output" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Asyncloop behavior" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Asynctimer behavior" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncTimerloggerTypeFromString testExpected.LoggerData = asyncTimerLoggerData{101} testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Rolling file writer size" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriter, _ := newRollingFileWriterSize(testLogFileName, rollingArchiveNone, "", 100, 5, rollingNameModePostfix) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testrollingFileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Rolling file writer archive zip" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriter, _ = newRollingFileWriterSize(testLogFileName, rollingArchiveZip, "log.zip", 100, 5, rollingNameModePostfix) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testrollingFileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Rolling file writer archive zip with specified path" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriter, _ = newRollingFileWriterSize(testLogFileName, rollingArchiveZip, "test.zip", 100, 5, rollingNameModePrefix) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testrollingFileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Rolling file writer archive none" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriter, _ = newRollingFileWriterSize(testLogFileName, rollingArchiveNone, "", 100, 5, rollingNameModePostfix) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testrollingFileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Rolling file writer date" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriterTime, _ := newRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingIntervalAny, rollingNameModePostfix) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testrollingFileWriterTime}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Buffered writer" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriterTime, _ = newRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingIntervalDaily, rollingNameModePostfix) testbufferedWriter, _ := newBufferedWriter(testrollingFileWriterTime, 100500, 100) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testbufferedWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Inner splitter output" testLogFileName1 := getTestFileName(testName, "1") testLogFileName2 := getTestFileName(testName, "2") testLogFileName3 := getTestFileName(testName, "3") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter1, _ := newFileWriter(testLogFileName2) testfileWriter2, _ := newFileWriter(testLogFileName3) testInnerSplitter, _ := newSplitDispatcher(defaultformatter, []interface{}{testfileWriter1, testfileWriter2}) testfileWriter, _ = newFileWriter(testLogFileName1) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testfileWriter, testInnerSplitter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) RegisterReceiver("custom-name-1", &customTestReceiver{}) testName = "Custom receiver 1" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testCustomReceiver, _ := newCustomReceiverDispatcher(defaultformatter, "custom-name-1", CustomReceiverInitArgs{ XmlCustomAttrs: map[string]string{ "test": "set", }, }) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testCustomReceiver}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Custom receiver 2" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil crec := &customTestReceiver{} cargs := CustomReceiverInitArgs{ XmlCustomAttrs: map[string]string{ "test": "set2", }, } crec.AfterParse(cargs) testCustomReceiver2, _ := newCustomReceiverDispatcherByValue(defaultformatter, crec, "custom-name-2", cargs) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testCustomReceiver2}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter fnc := func(initArgs CustomReceiverInitArgs) (CustomReceiver, error) { return &customTestReceiver{}, nil } cfg := CfgParseParams{ CustomReceiverProducers: map[string]CustomReceiverProducer{ "custom-name-2": CustomReceiverProducer(fnc), }, } testExpected.Params = &cfg parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, &cfg}) RegisterReceiver("-", &customTestReceiver{}) testName = "Custom receiver 3" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil creccustom := &customTestReceiver{} cargs3 := CustomReceiverInitArgs{ XmlCustomAttrs: map[string]string{ "test": "set3", }, } creccustom.AfterParse(cargs3) testCustomReceiver, _ = newCustomReceiverDispatcherByValue(defaultformatter, creccustom, "-", cargs3) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testCustomReceiver}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Custom receivers with formats" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testCustomReceivers := make([]*customReceiverDispatcher, 3) for i := 0; i < 3; i++ { testCustomReceivers[i], _ = newCustomReceiverDispatcher(defaultformatter, "custom-name-1", CustomReceiverInitArgs{ XmlCustomAttrs: map[string]string{ "test": fmt.Sprintf("set%d", i+1), }, }) } testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testCustomReceivers[0], testCustomReceivers[1], testCustomReceivers[2]}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Format" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter, _ = newFileWriter(testLogFileName) testFormat, _ := newFormatter("%Level %Msg %File") testHeadSplitter, _ = newSplitDispatcher(testFormat, []interface{}{testfileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Format2" testLogFileName = getTestFileName(testName, "") testLogFileName1 = getTestFileName(testName, "1") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter, _ = newFileWriter(testLogFileName) testfileWriter1, _ = newFileWriter(testLogFileName1) testFormat1, _ := newFormatter("%Level %Msg %File") testFormat2, _ := newFormatter("%l %Msg") formattedWriter, _ := newFormattedWriter(testfileWriter1, testFormat2) testHeadSplitter, _ = newSplitDispatcher(testFormat1, []interface{}{testfileWriter, formattedWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Minlevel = warn" testConfig = `` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(WarnLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Maxlevel = trace" testConfig = `` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, TraceLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Level between info and error" testConfig = `` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(InfoLvl, ErrorLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Off with minlevel" testConfig = `` testExpected = new(logConfig) testExpected.Constraints, _ = newOffConstraints() testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Off with levels" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Levels list" testConfig = `` testExpected = new(logConfig) testExpected.Constraints, _ = newListConstraints([]LogLevel{ DebugLvl, InfoLvl, CriticalLvl}) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = asyncLooploggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Errors #1" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #2" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #3" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #4" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #5" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #6" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #7" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #8" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #9" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #10" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #11" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #12" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #13" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #14" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #15" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #16" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #17" testLogFileName = getTestFileName(testName, "") testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #18" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #19" testConfig = `` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Exceptions: restricting" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) listConstraint, _ := newOffConstraints() exception, _ := newLogLevelException("Test*", "someFile.go", listConstraint) testExpected.Exceptions = []*logLevelException{exception} testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Exceptions: allowing #1" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newListConstraints([]LogLevel{ErrorLvl}) minMaxConstraint, _ := newMinMaxConstraints(TraceLvl, CriticalLvl) exception, _ = newLogLevelException("*", "testfile.go", minMaxConstraint) testExpected.Exceptions = []*logLevelException{exception} testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Exceptions: allowing #2" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newOffConstraints() minMaxConstraint, _ = newMinMaxConstraints(WarnLvl, CriticalLvl) exception, _ = newLogLevelException("*", "testfile.go", minMaxConstraint) testExpected.Exceptions = []*logLevelException{exception} testconsoleWriter, _ = newConsoleWriter() testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Predefined formats" formatID := predefinedPrefix + "xml-debug-short" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testconsoleWriter, _ = newConsoleWriter() testFormat, _ = predefinedFormats[formatID] testHeadSplitter, _ = newSplitDispatcher(testFormat, []interface{}{testconsoleWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Predefined formats redefine" testLogFileName = getTestFileName(testName, "") formatID = predefinedPrefix + "xml-debug-short" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testfileWriter, _ = newFileWriter(testLogFileName) testFormat, _ = newFormatter("%Level %Msg %File") testHeadSplitter, _ = newSplitDispatcher(testFormat, []interface{}{testfileWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Conn writer 1" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testConnWriter := newConnWriter("tcp", ":8888", false) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testConnWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Conn writer 2" testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testConnWriter = newConnWriter("tcp", ":8888", true) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{testConnWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) testName = "Errors #11" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #12" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #13" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #14" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #15" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #16" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #17" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #18" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #19" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #20" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #21" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #22" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #23" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #24" testLogFileName = getTestFileName(testName, "") testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #25" testLogFileName = getTestFileName(testName, "") testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Errors #26" testConfig = ` ` parserTests = append(parserTests, parserTest{testName, testConfig, nil, true, nil}) testName = "Buffered writer same formatid override" testLogFileName = getTestFileName(testName, "") testConfig = ` ` testExpected = new(logConfig) testExpected.Constraints, _ = newMinMaxConstraints(TraceLvl, CriticalLvl) testExpected.Exceptions = nil testrollingFileWriterTime, _ = newRollingFileWriterTime(testLogFileName, rollingArchiveNone, "", 0, "2006-01-02T15:04:05Z07:00", rollingIntervalDaily, rollingNameModePrefix) testbufferedWriter, _ = newBufferedWriter(testrollingFileWriterTime, 100500, 100) testFormat, _ = newFormatter("%Level %Msg %File 123") formattedWriter, _ = newFormattedWriter(testbufferedWriter, testFormat) testHeadSplitter, _ = newSplitDispatcher(defaultformatter, []interface{}{formattedWriter}) testExpected.LogType = syncloggerTypeFromString testExpected.RootDispatcher = testHeadSplitter parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil}) } return parserTests } // Temporary solution: compare by string identity. Not the best solution in // terms of performance, but a valid one in terms of comparison, because // every seelog dispatcher/receiver must have a valid String() func // that fully represents its internal parameters. func configsAreEqual(conf1 *logConfig, conf2 interface{}) bool { if conf1 == nil { return conf2 == nil } if conf2 == nil { return conf1 == nil } // logConfig, ok := conf2 //.(*logConfig) // if !ok { // return false // } return fmt.Sprintf("%v", conf1) == fmt.Sprintf("%v", conf2) //logConfig) } func testLogFileFilter(fn string) bool { return ".log" == filepath.Ext(fn) } func cleanupAfterCfgTest(t *testing.T) { toDel, err := getDirFilePaths(".", testLogFileFilter, true) if nil != err { t.Fatal("Cannot list files in test directory!") } for _, p := range toDel { err = tryRemoveFile(p) if nil != err { t.Errorf("cannot remove file %s in test directory: %s", p, err.Error()) } } } func parseTest(test parserTest, t *testing.T) { conf, err := configFromReaderWithConfig(strings.NewReader(test.config), test.parserConfig) if /*err != nil &&*/ conf != nil && conf.RootDispatcher != nil { defer func() { if err = conf.RootDispatcher.Close(); err != nil { t.Errorf("\n----ERROR while closing root dispatcher in %s test: %s", test.testName, err) } }() } if (err != nil) != test.errorExpected { t.Errorf("\n----ERROR in %s:\nConfig: %s\n* Expected error:%t. Got error: %t\n", test.testName, test.config, test.errorExpected, (err != nil)) if err != nil { t.Logf("%s\n", err.Error()) } return } if err == nil && !configsAreEqual(conf, test.expected) { t.Errorf("\n----ERROR in %s:\nConfig: %s\n* Expected: %v. \n* Got: %v\n", test.testName, test.config, test.expected, conf) } } func TestParser(t *testing.T) { defer cleanupAfterCfgTest(t) for _, test := range getParserTests() { parseTest(test, t) } }