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.
Conditional configuration (if statement) requires Janino. Is Janino available on your class path? Have you set the debug attribute to true as follows?
<configuration debug="true">...</configuration>
Setting the debug attribute to true will print logback's internal status messages on the console which can be very useful in tracking down logback configuration problems.
As for the separation question, have you considered separation by hostname? Logback automatically defines HOSTNAME as a variable. So the following would define two separate logging settings based on productionHost and other hosts.
<if condition='property("HOSTNAME").contains("productionHost")'>
<then>...</then>
<else>config for test</else>
</if>
Actually, I don't see why the separation according to the definition of 'logDir' is insufficient for achieving separation.
I would suggest to have separate module for the integration tests where you can put a different log file configuration (src/test/resources) and the configuration for the unit tests will be coming out of the module where you have put the unit tests into.
来源:https://stackoverflow.com/questions/10819791/different-logfile-for-integration-testing