根据测试配置,配置logback.xml以在一个文件或每个测试的单独文件中输出所有日志消息

时间:2017-10-09 07:25:12

标签: java logging junit logback

我有五个JUnit测试要在循环中运行。 (意识到JUnit参数化但暂时不使用它)

我想要的是,在运行测试之前,它会检查配置文件,其中有一个选项可以separeLogFile=true/false。如果该选项为true,则Test应为每个Test生成单独的日志文件,如果该选项为false,则将为所有五个Tests生成一个日志文件。

我使用logback.xml和SL4J记录器进行记录。目前,它只为所有测试生成一个日志文件。

我很困惑如何以编程方式指定根据选项生成日志文件的位置/位置?有什么方法可以指定两个文件名和位置,并根据配置文件中的选项使logback.xml从中选择吗?我第一次使用logback.xml

目前我的logback.xml如下:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </encoder>
</appender>


<appender name="FILE"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- Daily rollover -->
        <fileNamePattern>target/logs/Log_Test_%d{yyyy-MM-dd}.log
        </fileNamePattern>

        <!-- Keep 7 days' worth of history -->
        <maxHistory>7</maxHistory>
    </rollingPolicy>

    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<!-- Configure so that it outputs to both console and log file -->
<root level="INFO">
    <appender-ref ref="FILE"/>
    <appender-ref ref="STDOUT"/>
</root>

1 个答案:

答案 0 :(得分:1)

您可以使用Logback的SiftingAppender将日志定向到特定的日志文件。

以下是一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="SIFTER" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>testName</key>
            <defaultValue>ALL</defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE-${testName}" class="ch.qos.logback.core.FileAppender">
                <file>target/logs/Log_Test_${testName}.log</file>

                <encoder>
                    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
                    </pattern>
                </encoder>
            </appender>
        </sift>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="SIFTER"/>
    </root>

</configuration>

使用此Logback配置,以下代码...

String[] testNames = new String[] { "one", "two"};

for (String testName : testNames) {
    MDC.put("testName", testName);
    logger.info("This message should go into the log file for {}", testName);
}

...将导致两个日志文件被填充:

~/dev/code/stackoverflow/target/logs$ cat Log_Test_one.log 
09:04:19.922 [main] INFO  SomeClassTest - This message should go into the log file for one
~/dev/code/stackoverflow/target/logs$ cat Log_Test_two.log 
09:04:19.938 [main] INFO  SomeClassTest - This message should go into the log file for two

以下代码......

String[] testNames = new String[] { "one", "two"};

for (String testName : testNames) {
    logger.info("This message should go into the log file for {}", testName);
}

...将导致一个日志文件被填充:

~/dev/code/stackoverflow/target/logs$ cat Log_Test_ALL.log 
09:06:54.009 [main] INFO  SomeClassTest - This message should go into the log file for one
09:06:54.022 [main] INFO  SomeClassTest - This message should go into the log file for two

因此,如果在调用testName之前填充logger.xxx() MDC属性,则将筛选日志事件,即定向到以该MDC值命名的日志文件。如果您没有填充testName MDC属性,则所有日志事件都将定向到“ALL”日志文件。

更新1 以回应此评论:

  

我用你提供的文件重新整理了我原来的File Appender的整个块,但它根本没有生成日志文件。有些东西出错了。

如果没有看到所有配置,很难诊断出错误,所以我已经更新了这个答案以包含一个完整的logback.xml,并且此配置已启用调试,因此您应该看到输出... ...

09:59:32,268 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [SIFTER]
09:59:32,281 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.sift.MDCBasedDiscriminator] for [discriminator] property
09:59:32,298 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
09:59:32,298 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
09:59:32,304 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [SIFTER] to Logger[ROOT]
...
09:59:34,725 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
09:59:34,730 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-one]
...
09:59:34,732 |-INFO in ch.qos.logback.core.FileAppender[FILE-one] - File property is set to [target/logs/Log_Test_one.log]

...显示了筛选的追随者。