tuk tuk - 4 months ago 22
Java Question

Override log4j.properties during testing

I want to log only the

ERROR
during testing in maven. For this purpose I created a file
src/test/resources/log4j.properties
, which is going to override the configuration I already have in
src/main/resources/log4j.properties
. But this is not working. My
log4j.properties
looks like below:-

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


My
pom.xml
looks like below:-

<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>


But setting log level to
ERROR
or some other value in
log4j.properties
is not making any effect. The actual level is always
INFO
. I have gone through this question and tried modifying my sure-fire plugin configuration but still no luck.

<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 version is as below:-

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"


The issue is there only in test config and during the test execution. If the log level is changed in
src/main/resources/log4j.properties
then during the main application run the log level messages are fine.

Can someone let me know what is going wrong?

tuk tuk
Answer

The issue was because one of the dependency (s3ninja) we were using during testing. It seems it was also using log4j and has its default log level set to INFO. To solve this I also set s3ninja's log level to the rootLogger level that is set in src/test/resources/log4j.properties. Something like below:-

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