我在用SL4j http://www.slf4j.org/ and Logback http://logback.qos.ch/用于 Tomcat 中托管的 Web 应用程序。我使用 Spring 和 Maven(无配置文件)。集成测试是使用 Surefire 插件完成的:
<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>
在 logback 配置中,我有一个基于文件的附加程序:
<appender name="A2" class="ch.qos.logback.core.FileAppender">
<file>myapp.log</file>
...
巧合的是,集成测试和 Web 应用程序的日志文件是分开的:对于集成测试,它是我的项目的根目录,对于 Web 应用程序,它是 Eclipse 目录。所以我在 logback 配置中引入了一个日志文件位置:
<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
结合isDefined
现在可以工作了,我忘记了 Janino 在类路径上(感谢 Ceki)。集成测试日志输出和 Web 应用程序日志输出都在同一日志文件中。所以我的问题是:
如何分离集成测试 Web 应用程序的日志文件?
我找到了这个link http://www.nullin.com/2010/07/28/logging-tests-to-separate-files/,但我更喜欢仅包含配置的解决方案。我真的很想插入 Maven 属性。
Update我的问题解决了。首先是logback配置:
<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>
附加程序文件属性如下所示:
<file>${logDir}/myapp.log</file>
这个解决方案中有两件事很奇怪:
- 当属性为空时,logback 认为属性未定义。所以我不得不使用
"./"
代替""
(空字符串)。
-
isDefined("catalina.home")
结果true
仅当在 Tomcat 中启动时(操作系统为 Windows)。无论如何,不知道为什么要定义“catalina.home”,我有一个名为“CATALINA_HOME”的环境变量,但看起来 TomCat 在启动时设置了“catalina.home”。
我仍然想将 Maven var 插入到 logback 配置(项目根目录)中,但恐怕我必须接受上面的解决方案。