Reputation: 385
I want to have two Log4j config files in my web-application. The first one I placed here: conf/main/log4j.properties
The second is here: conf/test/resources/log4j-test.properties
so my pom contains the following:
<build>....
<resources>
<resource>
<directory>conf/main</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>conf/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</testResource>
</testResources>
.......
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>file:${project.build.testOutputDirectory}/log4j-test.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
.....
log4j.properties:
log4j.rootLogger=INFO, stdout, mainFile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.mainFile=org.apache.log4j.RollingFileAppender
log4j.appender.mainFile.File=../logs/main.log
log4j.appender.mainFile.MaxFileSize=1MB
log4j.appender.mainFile.MaxBackupIndex=1
log4j.appender.mainFile.layout=org.apache.log4j.PatternLayout
log4j.appender.mainFile.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
and log4j-test.properties:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
My goal - to write logs from tests only to my console. For example I have a simple test method:
@Test(dataProvider = "invalidCredentials", threadPoolSize = 10, invocationCount = 1, invocationTimeOut = 1000)
public void testGetUserByLoginInvalid(String login, String password) {
LDAPUser user = LdapUtils.getUserByLogin(login, password);
assertNull(user);
}
Class LdapUtils has logger definition:
private static final Logger logger = Logger.getLogger(LdapUtils.class);
and it can write some info-message:
public static LDAPUser getUserByLogin(String login, String password) {
....
if (user == null) {
logger.info(INVALID_USER + login);
return null;
}
.....
}
So the problem is: when I execute Maven install(or simply - test) it works fine because it reads log4j config from right place (conf/test/resources/log4j-test.properties) and writes nothing to file, only to console.
But when I'm trying to run this test method in IntellijIDEA (righ click on testGetUserByLoginInvalid method and choose Run "testGetUserByLoginInvalid()" ) it fails with
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: ..\logs\main.log (The system cannot find the path specified)
In this case it reads wrong config(log4j.properties instead of log4j-test.properties) and fails when trying to find ..\logs\main.log file
log4j.appender.mainFile.File=../logs/main.log
in case of web-application it seems to be just the same as
log4j.appender.mainFile.File=${catalina.home}/logs/main.log
But tests do not have to know something about ${catalina.home} at all, isn't it? Because they are not part of .war. So my idea was to create its own log4j config for tests but I can't avoid this problem with reading wrong config when running test not with Maven but with IDEA as I've described above...
Any idea's anyone? Or may be there's another way to do what I want?... Thank you very much in advance!
Upvotes: 4
Views: 4120
Reputation: 48943
I resolve issue with ${catalina.home} by:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemProperties>
<property>
<name>catalina.home</name>
<value>${project.build.directory}</value>
</property>
</systemProperties>
</configuration>
</plugin>
Upvotes: 2