This post comes from my frustration with the following error messages when running tests:
Background
For logging purposes, I use log4j.
Most of the services I write, and that are started with a command line interface, are using the -Dlog4j.configuration=yyy
configuration property to point to the proper log4j configuration. So it works.
The issue is that when you run tests for the same code that uses log4j, the tests are started differently, whether it is in your IDE (in my case IDEA) or using the build framework (in my case gradle
): they do not have this property provided. So you end up with this log4j warning message. The big issue though is not really the warning message, it is the fact that for some odd reason, log4j just swallows any single output moving forward, which I am not entirely sure is the smartest way to handle it.
Solution
Here is what I did to resolve the issue (I am sure there are other ways, but this one works and is pretty simple):
Create a log4j project
In your multi-project build (gradle
), create a brand new project:
This is the content of log4j.xml
:
and this is the content of build.gradle
Add a test dependency in your other projects
Thanks to gradle
support for multi-project build, in any project in which you have tests, simply add a (testRuntime
) dependency to the log4j-test-config
project:
Running tests
Now when you run your tests with gradle
, it will automatically pick up the jar file generated by the log4j-test-config
project and add it to the classpath => the file log4j.xml
will be on the classpath and log4j will be happy.
If you issue gradle cleanIdea idea
then gradle will rebuild the IDEA project and add the proper dependency within the ide itself => when you run your test directly in IDEA, then the same happens and you get log4j output!
warn
in the log4j.xml
file with any other value during testing/debugging and it will automatically be picked up by IDEA or gradle…