用于集成测试的不同日志文件 [英] Different logfile for integration testing

查看:82
本文介绍了用于集成测试的不同日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SL4j 登录,用于托管在Tomcat中的Web应用程序.我使用Spring和Maven(无配置文件).集成测试是通过Surefire插件完成的:

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>

在logback配置中,我有一个基于文件的附加程序:

Inside the logback configuration I have a file based appender:

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

用于集成测试的日志文件和webapp的巧合是分开的:对于集成测试,这是我项目的根,对于webapp,它是Eclipse目录.因此,我在logback配置中引入了一个日志文件位置:

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>

isDefined结合使用的if现在可以工作了,我在类路径上忘记了Janino(感谢Ceki).集成测试日志输出和Web应用程序日志输出都在同一日志文件中.所以我的问题是:

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:

如何为集成测试Web应用程序分隔日志文件? 我找到了这个链接,但是我会偏爱仅带有配置的解决方案.我真的很想插入Maven属性.

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>

appender文件的属性如下:

The appender file property looks like:

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

此解决方案中有2件事很奇怪:

2 things are strange in this solution:

  1. logback认为属性为空时未定义.因此,我不得不使用"./"而不是""(空字符串).
  2. isDefined("catalina.home")仅在Tomcat(操作系统是Windows)中启动时才会产生true.不知道为什么仍然定义了"catalina.home",我有一个名为"CATALINA_HOME"的环境变量,但是它似乎表明TomCat在启动时就设置了"catalina.home".
  1. logback thinks that a property is undefined when it is empty. So I had to use "./" instead of "" (empty string).
  2. isDefined("catalina.home") results true 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.

我仍然想将Maven var插入到logback配置(项目根目录)中,但是恐怕我必须接受上面的解决方案.

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.

推荐答案

条件配置(如果声明)需要Janino. Janino在您的上课路上有空吗?您是否已将debug属性设置为true?

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>

将debug属性设置为true将会在控制台上打印logback的内部状态消息,这对于跟踪logback配置问题非常有用.

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.

关于分隔问题,您是否考虑过按主机名分隔? Logback自动将HOSTNAME定义为变量.因此,以下将基于productionHost和其他主机定义两个单独的日志记录设置.

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>

实际上,我不明白为什么按照'logDir'的定义进行分离不足以实现分离.

Actually, I don't see why the separation according to the definition of 'logDir' is insufficient for achieving separation.

这篇关于用于集成测试的不同日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆