How to override log4j.properties during testing?
Asked Answered
R

3

18

I'm trying to log all DEBUG messages to console 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. Unfortunately such an overriding is not happening. Why and how to fix it?

Ruinous answered 5/11, 2010 at 13:59 Comment(3)
@Bozho I also think so, but looks like these two files conflict. When I have them two NO logging config works. They both are just ignored...Ruinous
I think maven just includes both resources in path, so log4j gets confused which one to use and instead just fails by not showing anything. Does Robert's solution work?Incubation
Bozho is right, it works fine as it is. The problem was with my logging configuration. I used slf4j-jdk14 instead of slf4j-log4j. Everything works fine without the trick offered by Robert.Ruinous
R
2

It should work as it is, and it works. The problem is somewhere else.

ps. I had a mess with loggers in my classpath: jog4j, slf4j, logback (from other dependencies). As I understand, all of them are in conflict. I didn't clean this mess yet, and I still don't know how to make all packages to use one logging facility and one configuration.

Ruinous answered 5/11, 2010 at 16:59 Comment(1)
How is that you didn't clean the mess. don't know how to make them all work together and get the correct answer?Nicknack
M
28

Rename your test configuration file to e.g. log4j-surefire.properties and configure log4j to pick it up during surefire execution:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <systemPropertyVariables>
            <log4j.configuration>file:${project.build.testOutputDirectory}/log4j-surefire.properties</log4j.configuration>
        </systemPropertyVariables>
    </configuration>
</plugin>
Moonfish answered 5/11, 2010 at 14:2 Comment(1)
I up-voted your answer, many thanks for it. Unfortunately, it is not helpful in my case, since the problem is not with the location of the file, but of log4j usage together with slf4j.Ruinous
R
2

It should work as it is, and it works. The problem is somewhere else.

ps. I had a mess with loggers in my classpath: jog4j, slf4j, logback (from other dependencies). As I understand, all of them are in conflict. I didn't clean this mess yet, and I still don't know how to make all packages to use one logging facility and one configuration.

Ruinous answered 5/11, 2010 at 16:59 Comment(1)
How is that you didn't clean the mess. don't know how to make them all work together and get the correct answer?Nicknack
J
0

Beware of changed version for that, with 2.17.1 of log4j at least, you'll have to change it from log4j.configuration to log4j.configurationFile to avoid having

ERROR StatusLogger Reconfiguration failed: No configuration found for '18b4aac2' at 'null' in 'null'

Here an example

        <plugin>
            <!--            DOC http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
            <!-- enables java assertions for tests -->
            <configuration>
                <enableAssertions>true</enableAssertions>
                <systemPropertyVariables>
                    <log4j.configurationFile>src/test/resources/log4j2_for_surefire.xml</log4j.configurationFile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
Josettejosey answered 8/2, 2022 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.