-->

不同的日志文件进行集成测试(Different logfile for integration te

2019-09-17 15:05发布

我使用SL4j和的logback在Tomcat的托管Web应用程序。 我使用Spring和Maven(没有配置文件)。 集成测试与神火插件来完成:

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

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

if在组合isDefined现在的工作,我忘了JANINO在classpath(感谢Ceki)。 这两种集成测试日志输出和Web应用程序日志输出在同一个日志文件。 所以我的问题:

我怎么能单独进行集成测试Web应用程序的日志文件? 我发现这个链接 ,但我宁愿只配置一个解决方案。 我真的很想插入Maven的性质。

更新我的问题就解决了。 首先配置的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>

2个东西都在这个解决方案奇怪:

  1. 的logback认为,当它是空的属性未定义。 所以我不得不使用"./" ,而不是"" (空字符串)。
  2. isDefined("catalina.home")结果true在Tomcat的(操作系统是Windows)开始,只有当。 不知道为什么“的catalina.home”无论如何定义,我有一个名为“CATALINA_HOME”环境无功,但它的接缝Tomcat是设置“的catalina.home”上的开始。

我还是想插入一个Maven VAR到的logback配置(项目的根),但我恐怕不得不忍受上述解决方案。

Answer 1:

有条件的配置(if语句)需要JANINO。 是JANINO提供您的类路径? 你有没有调试属性设置为true,如下所示?

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

设置调试属性为true,将打印的可追查的logback配置问题非常有用的控制台上的logback的内部状态信息。

作为分离的问题,你有没有考虑通过主机分离? 的logback自动HOSTNAME定义为一个变量。 所以下面将定义基于productionHost和其他主机两个独立的日志记录设置。

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

其实,我不明白为什么分离根据“LOGDIR”的定义是不足以实现分离。



Answer 2:

我会建议有集成测试,你可以把不同的日志文件配置中(src /测试/资源)和单元测试将现身,你已经把单元测试到模块的配置单独的模块。



文章来源: Different logfile for integration testing