Most of the time, we may have several Log4J configurations depending on the environments, for example:-
log4j2.xml(Log4J2) – Production configuration using socket appender.
log4j2-dev.xml(Log4J2) – Development configuration using console appender.
log4j2.xml are the default configuration files for Log4J and Log4J2, these configurations will always be used unless we override the configuration file path.
In another word, if we don’t override the configuration file path and we run our JUnit test cases offline from IntelliJ, it may take a very long time to execute them due to the broken socket connection. Further, it is not a good idea to clutter our production log files with non-production logs.
To fix this, we need to configure IntelliJ to always use our development Log4J configuration.
First, on the menu bar, select
Run -> Edit Configurations...
Then, delete all the existing JUnit configuration files.
Defaults -> JUnit.
Under VM options, specify the following system property:-
- For Log4J, specify
- For Log4J2, specify
Please note the slight change on the system property name depending on the Log4J version.
Now, when we run our JUnit test cases from IntelliJ, it will always pick up the correct custom Log4J configuration file.