在测试期间覆盖 log4j.properties

Override log4j.properties during testing

我只想在 Maven 测试期间记录 ERROR。为此,我创建了一个文件 src/test/resources/log4j.properties,它将覆盖我在 src/main/resources/log4j.properties 中已有的配置。但这是行不通的。我的 log4j.properties 如下所示:-

log4j.rootLogger=ERROR, out, stdout

# CONSOLE appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %X | %m%n
#log4j.logger.org.apache.http=INFO

# File appender
log4j.appender.out=org.apache.log4j.DailyRollingFileAppender
log4j.appender.out.File=/tmp/wf_test.log
log4j.appender.out.DatePattern='.'yyyy-MM-dd
log4j.appender.out.Append=true
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %X | %m%n

我的 pom.xml 如下所示:-

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jabong.orchestratorservice</groupId>
    <artifactId>basecomponent</artifactId>
    <version>0.0.1</version>
    <packaging>bundle</packaging>
    <name>basecomponent</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <camel.version>2.16.1</camel.version>
        <jackson.version>2.7.4</jackson.version>
        <jackson.afterburner.version>2.7.1</jackson.afterburner.version>
        <runSuite>**/AllTestSuite.class</runSuite>
        <sonar.host.url>http://sonar:9000/</sonar.host.url>
        <sonar.jdbc.username>sonar</sonar.jdbc.username>
        <sonar.jdbc.password>sonar</sonar.jdbc.password>
        <sonar.jdbc.url>jdbc:mysql://sonar:3306/sonar?useUnicode=true&amp;characterEncoding=utf8</sonar.jdbc.url>
        <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>      
        <sonar.binaries>target</sonar.binaries>
        <sonar.tests>src/test</sonar.tests>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.junit.reportsPath>build/test-reports</sonar.junit.reportsPath>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-afterburner</artifactId>
            <version>${jackson.afterburner.version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.arnabk</groupId>
            <artifactId>java-dogstatsd-client</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20151123</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.im4java</groupId>
            <artifactId>im4java</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>
                com.googlecode.maven-java-formatter-plugin
            </groupId>
            <artifactId>maven-java-formatter-plugin</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jdbc</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jetty</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-aws</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test</artifactId>
            <version>${camel.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.tomakehurst</groupId>
            <artifactId>wiremock</artifactId>
            <version>1.58</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.javacrumbs.json-unit</groupId>
            <artifactId>json-unit</artifactId>
            <version>1.11.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.scireum</groupId>
            <artifactId>s3ninja</artifactId>
            <version>2.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <activation>
                <property>
                    <name>!maven.test.skip</name>
                </property>
            </activation>

            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <excludes>
                            <exclude>**/*.cfg</exclude>
                        </excludes>
                    </resource>
                </resources>
                <testResources>
                    <testResource>
                        <directory>${project.basedir}/src/test/resources/</directory>
                    </testResource>
                    <testResource>
                        <directory>${project.basedir}/src/test/resources/config/</directory>
                    </testResource>
                </testResources>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>3.3</version>
                            <configuration>
                                <source>1.8</source>
                                <target>1.8</target>
                            </configuration>
                        </plugin>
                    </plugins>
                </pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <includes>
                                <include>${runSuite}</include>
                            </includes>
                            <!-- <skipTests>true</skipTests> -->
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.7.0.201403182114</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                        </configuration>
                    </plugin>
                    <!-- Documentation at https://karaf.apache.org/manual/latest/developers-guide/karaf-maven-plugin.html -->
                    <plugin>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>maven-bundle-plugin</artifactId>
                        <version>3.0.1</version>
                        <extensions>true</extensions>
                        <configuration>
                            <instructions>
                                <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                                <Bundle-Description>${project.description}</Bundle-Description>
                                <Import-Package>*;resolution:=optional</Import-Package>
                            </instructions>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>    
</project>

但是将日志级别设置为 ERRORlog4j.properties 中的某个其他值没有任何效果。实际水平总是INFO。我已经完成了这个 question 并尝试修改我的 sure-fire 插件配置,但仍然没有成功。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <includes>
            <include>${runSuite}</include>
        </includes>
        <systemPropertyVariables>
            <log4j.configuration>file:src/test/resources/log4j-surefire.properties</log4j.configuration>
        </systemPropertyVariables>
    </configuration>
</plugin>

Maven版本如下:-

jabongs-MacBook-Pro-4:basecomponent debraj$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /Users/debraj/Downloads/apache-maven-3.3.9
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"

问题只存在于测试配置和测试执行期间。如果在 src/main/resources/log4j.properties 中更改了日志级别,那么在主应用程序期间 运行 日志级别消息是正常的。

谁能告诉我出了什么问题?

问题是因为我们在测试期间使用的依赖项之一 (s3ninja)。它似乎也在使用 log4j,并将其默认日志级别设置为 INFO。为了解决这个问题,我还将 s3ninja 的日志级别设置为 src/test/resources/log4j.properties 中设置的 rootLogger 级别。如下所示:-

Setup setup = new Setup(Mode.TEST, ClassLoader.getSystemClassLoader());
setup.withDefaultLogLevel(Logger.getRootLogger().getLevel());
Sirius.start(setup);