Java 我在哪里可以在 JUnit 测试类中配置 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/878348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:33:21  来源:igfitidea点击:

Where do I configure log4j in a JUnit test class?

javaloggingjunitlog4j

提问by skiphoppy

Looking at the last JUnit test case I wrote, I called log4j's BasicConfigurator.configure() method inside the class constructor. That worked fine for running just that single class from Eclipse's "run as JUnit test case" command. But I realize it's incorrect: I'm pretty sure our main test suite runs all of these classes from one process, and therefore log4j configuration should be happening higher up somewhere.

查看我编写的最后一个 JUnit 测试用例,我在类构造函数中调用了 log4j 的 BasicConfigurator.configure() 方法。这适用于从 Eclipse 的“作为 JUnit 测试用例运行”命令运行单个类。但我意识到这是不正确的:我很确定我们的主要测试套件从一个进程运行所有这些类,因此 log4j 配置应该发生在更高的某个地方。

But I still need to run a test case by itself some times, in which case I want log4j configured. Where should I put the configuration call so that it gets run when the test case runs standalone, but not when the test case is run as part of a larger suite?

但是有时我仍然需要自己运行一个测试用例,在这种情况下我需要配置 log4j。我应该把配置调用放在哪里,以便它在测试用例独立运行时运行,而不是在测试用例作为更大套件的一部分运行时运行?

采纳答案by Paul Morie

The LogManagerclass determines which log4j config to use in a static blockwhich runs when the class is loaded. There are three options intended for end-users:

所述LogManager类确定的log4j配置在一个使用静块被加载的类时运行。为最终用户提供了三个选项:

  1. If you specify log4j.defaultInitOverrideto false, it will not configure log4j at all.
  2. Specify the path to the configuration file manually yourself and override the classpath search. You can specify the location of the configuration file directly by using the following argument to java:

    -Dlog4j.configuration=<path to properties file>

    in your test runner configuration.

  3. Allow log4j to scan the classpath for a log4j config file during your test. (the default)

  1. 如果指定log4j.defaultInitOverride为 false,则根本不会配置 log4j。
  2. 自己手动指定配置文件的路径并覆盖类路径搜索。您可以使用以下参数直接指定配置文件的位置java

    -Dlog4j.configuration=<path to properties file>

    在您的测试运行器配置中。

  3. 允许 log4j 在测试期间扫描 log4j 配置文件的类路径。(默认)

See also the online documentation.

另请参阅联机文档

回答by James McMahon

You may want to look into to Simple Logging Facade for Java (SLF4J). It is a facade that wraps around Log4j that doesn't require an initial setup call like Log4j. It is also fairly easy to switch out Log4j for Slf4j as the API differences are minimal.

您可能需要查看Simple Logging Facade for Java (SLF4J)。它是一个围绕 Log4j 的外观,不需要像 Log4j 这样的初始设置调用。由于 API 差异很小,因此将 Log4j 切换为 Slf4j 也相当容易。

回答by araqnid

I generally just put a log4j.xml file into src/test/resources and let log4j find it by itself: no code required, the default log4j initialisation will pick it up. (I typically want to set my own loggers to 'DEBUG' anyway)

我通常只是将一个 log4j.xml 文件放入 src/test/resources 并让 log4j 自己找到它:不需要代码,默认的 log4j 初始化会选择它。(无论如何,我通常想将自己的记录器设置为“调试”)

回答by gavenkoa

I use system properties in log4j.xml:

我在 log4j.xml 中使用系统属性:

...
<param name="File" value="${catalina.home}/logs/root.log"/>
...

and start tests with:

并开始测试:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <systemProperties>
            <property>
                <name>catalina.home</name>
                <value>${project.build.directory}</value>
            </property>
        </systemProperties>
    </configuration>
</plugin>