Reputation: 5239
We use the following log4j.xml
configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{ISO8601}] %-5p [%c{1}]#[%t] %m %n"/>
</layout>
</appender>
<category name="some.internal.enterprise.custom.library">
<priority value="DEBUG"/>
</category>
<!-- Removing the useless logging of "Invalid JavaBean property" -->
<logger name="org.springframework.beans.GenericTypeAwarePropertyDescriptor">
<level value="ERROR"/>
</logger>
<logger name="org.springframework">
<level value="WARN"/>
</logger>
<root>
<priority value="INFO"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
And we set up the JUnit test class for logging this way:
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
// more imports...
@RunWith(JUnitParamsRunner.class)
@ContextConfiguration(locations = {ProxyConfiguration.SPRING_CONTEXT_CONFIGURATION_LOCATIONS})
@Category(Integration.class)
@FixMethodOrder(MethodSorters.JVM)
public class MyTest {
private final Logger log = LogManager.getLogger(MyTest.class);
@Test
public void test() {
log.info("Blah blah");
}
}
I would expect Maven's Console Output
to display this log, but it doesn't. Yet it appears when I run the test locally on IntelliJ with a Run Configuration of type JUnit
.
What am I supposed to do to get my logs to show up in the Console Output
?
The answers I find online mention wanting to see the logging even when the tests succeed: that would also be a requirement of mine. However, their questions implied that it was already logging when their tests were failing, yet that's not what is happening here. The only thing I get from my test is the stacktrace when an Exception is thrown.
Upvotes: 0
Views: 2411
Reputation: 5239
A colleague made me realize that the logs were being output in target/surefire-reports/my.package.TestClassName-output.txt
.
Turns out the redirectTestOutputToFile
flag of our maven-surefire-plugin
was set to true
. I found that out by reading the output of mvn clean verify --debug
.
So the pom.xml
ends up looking something like that (I've removed a whole bunch of irrelevant stuff):
<project>
<properties>
<slf4j.version>1.7.28</slf4j.version>
</properties>
<dependencies>
<!-- Make sure you exclude any conflicting versions from your other dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>${logToFile}</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
And now you can use logToFile
JVM argument to manipulate where the logging goes. Not providing any value for that variable will have it default to false
, which is what we wanted. Alternatively, we have the option to output to a report file with mvn clean verify -DlogToFile=true
.
The documentation concerning surefire's flags is there: https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#redirectTestOutputToFile
Upvotes: 2