I am using SL4j and Logback for a web application hosted in Tomcat. I use Spring and Maven (no profiles). Integration testing is done with the Surefire plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>...</configuration>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Inside the logback configuration I have a file based appender:
<appender name="A2" class="ch.qos.logback.core.FileAppender">
<file>myapp.log</file>
...
The log files for integration test and the webapp have been seperated by coincidence: for the integration test it was the root of my project, for the webapp it was the Eclipse directory. So I introduced a log file location inside the logback config:
<insertFromJNDI env-entry-name="java:comp/bla/logDir" as="logDir" />
<if condition='!isDefined("logDir")'>
<then>
<property name="logDir" value="${catalina.home}/logs/" />
</then>
</if>
The if
in combination with isDefined
works now, I forgot Janino on the classpath (thanks to Ceki). Both integration test log output and web application log output in the same log file. So my question:
How could I seperate the log files for integration test web application? I found this link, but I would prefer a solution with configuration only. I really would love to insert Maven properties.
Update My problem is solved. First the logback config:
<configuration scan="true" debug="true">
<!-- used for the production webapp -->
<insertFromJNDI env-entry-name="java:comp/bla/logDir" as="logDir" />
<if condition='!isDefined("logDir")'>
<then>
<if condition='isDefined("catalina.home")'>
<then>
<!-- used for the development webapp -->
<property name="logDir" value="${catalina.home}/logs/" />
</then>
<else>
<!-- used for the integration test -->
<property name="logDir" value="./" />
</else>
</if>
</then>
</if>
The appender file property looks like:
<file>${logDir}/myapp.log</file>
2 things are strange in this solution:
- logback thinks that a property is undefined when it is empty. So I had to use
"./"
instead of""
(empty string). isDefined("catalina.home")
resultstrue
only when started in Tomcat (OS is Windows). Don't know why "catalina.home" is defined anyway, I have a environment var called "CATALINA_HOME", but it seams that TomCat is setting "catalina.home" on start.
I still would like to insert a Maven var into the logback config (the project root), but I am afraid I have to live with the solution above.