SpringJUnit4ClassRunner plus Log4j2 plus log4j2.xml outside class pathpath
I have several tests that I am running with SpringJUnit4ClassRunner
. I also just switched to Log4j2 and now I'm having problems loading my config file log4j2.xml
, I always get this error:
ERROR StatusLogger No log4j2 configuration file found. Using default
configuration: logging only errors to the console.
The problem is that the config file is not on the classpath. Instead, I save it in a separate folder along with other configuration data outside of the class. When I run the test, I have already installed -Dconfig=/path/to/config/folder
. I know that I can also pass -Dlog4j.configurationFile=...
when running tests, but I don't want to.
What I want to do is use Spring's annotation based construct to write a config class in which I programmatically set the location for Log4j. This is possible if I have a web application , but I don't know how to apply it to SpringJUnit4ClassRunner
.
@Configuration
public class Log4jConfiguration {
/** Initialize Log4j2 with a custom location for the log4j2.xml **/
}
source to share
Maybe your Maven, specify:
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include><!---->
</includes>
<filtering>false</filtering>
</resource>
</resources>
Check it.
source to share