3

I have some tests that I run with SpringJUnit4ClassRunner. I also just migrated to Log4j2 and now I have trouble loading my log4j2.xml configuration file, 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 configuration file is not on the classpath. Instead I keep it in a separate folder together with other configuration data outside of the classpath. When I start the test I already set -Dconfig=/path/to/config/folder. I know I can also pass -Dlog4j.configurationFile=... when I run the tests, but I do not want to do that.

What I want to do is use Spring annotation based configuration to write a configuration class in which I programatically set the location for Log4j. This is possible if I have a web application, but I don't know how to apply this to SpringJUnit4ClassRunner.

@Configuration
public class Log4jConfiguration {
    /** Initialize Log4j2 with a custom location for the log4j2.xml **/
}
Community
  • 1
  • 1
lanoxx
  • 12,249
  • 13
  • 87
  • 142
  • By using `-Dlog4j.configurationFile=...` is exactly the same of what you are trying to achieve with `-Dconfig=/path/to/config/folder`. So, why not use the first option and rely on the **Automatic Configuration**? You don't really need to reinvent the wheel in this matter – x80486 May 02 '15 at 11:41
  • Because I have other configuration files in that folder, and lots of places in my code are relying on this, so I don't want to change it. – lanoxx May 02 '15 at 16:26

1 Answers1

0

May be 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.

Marshall Davis
  • 3,337
  • 5
  • 39
  • 47