Various log files for integration testing

I am using SL4j and Logback for a web application hosted in Tomcat. I use Spring and Maven (no profiles). Integration testing is performed using 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 application:

 <appender name="A2" class="ch.qos.logback.core.FileAppender"> <file>myapp.log</file> ... 

The log files for the integration test and webapp were separated by coincidence: for the integration test, this was the root of my project, for webapp it was the Eclipse directory. Thus, I presented the location of the log file in the log configuration:

 <insertFromJNDI env-entry-name="java:comp/bla/logDir" as="logDir" /> <if condition='!isDefined("logDir")'> <then> <property name="logDir" value="${catalina.home}/logs/" /> </then> </if> 

Now if combined with isDefined works, I forgot Janino on the way to classes (thanks Ceki). Both test log output and web application log output in the same log file. So my question is:

How can I separate log files for a web application for integration testing? I found the link, but I would prefer a solution with only customization. I really would like to add Maven properties.

Update My problem has been resolved. First log configuration:

 <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 this:

  <file>${logDir}/myapp.log</file> 

In this solution, it is strangely strange:

  • logback considers the property to be undefined when it is empty. So I had to use "./" instead of "" (empty string).
  • isDefined("catalina.home") results true only when launched in Tomcat (OS - Windows). I don't know why "catalina.home" is defined anyway, I have an environment variable called "CATALINA_HOME", but it flips that TomCat sets "catalina.home" at startup.

I would still like to insert Maven var in the logback configuration (project root), but I'm afraid that I will have to live with the solution above.

+4
spring maven logback janino
source share
2 answers

Conditional configuration (if instruction) requires Janino. Is Yanino available on your way to class? Have the debug attribute set to true as follows:

 <configuration debug="true">...</configuration> 

Setting the debug attribute to true will print internal log status messages to the console, which can be very useful when tracking log configuration problems.

Regarding the split issue, do you consider split by host name? Logback automatically defines HOSTNAME as a variable. Thus, the following definition of two separate logging options based on productionHost and other hosts.

 <if condition='property("HOSTNAME").contains("productionHost")'> <then>...</then> <else>config for test</else> </if> 

Actually, I donโ€™t understand why the separation according to the definition of "logDir" is not enough to achieve separation.

+3
source share

I would suggest having a separate module for integration tests, where you can put another configuration of the log file (src / test / resources), and the configuration for unit tests will exit the module in which you put the test block in.

0
source share

All Articles