Java Log4j 输出未显示在 Eclipse 控制台中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3501355/
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
Log4j output not displayed in Eclipse console
提问by javaExpert
For some reason my Eclipse console no longer displays Log4j INFO and DEBUG statements when I run JUnit tests. In terms of code there hasn't been any change, so it must something to do with the Eclipse configuration.
出于某种原因,当我运行 JUnit 测试时,我的 Eclipse 控制台不再显示 Log4j INFO 和 DEBUG 语句。在代码方面没有任何变化,所以它必须与 Eclipse 配置有关。
All I do in my Unit test is the following and for some reason ONLY the ERROR statement is displayed in the Eclipse console. Why? Where shall I look for clues?
我在单元测试中所做的一切如下,出于某种原因,Eclipse 控制台中仅显示 ERROR 语句。为什么?我该去哪里寻找线索?
public class SampleTest
{
private static final Logger LOGGER = Logger.getLogger(SampleTest.class);
@Before
public void init() throws Exception
{
// Log4J junit configuration.
BasicConfigurator.configure();
LOGGER.info("INFO TEST");
LOGGER.debug("DEBUG TEST");
LOGGER.error("ERROR TEST");
}
}
Details:
细节:
- log4j-1.2.6.jar
- junit-4.6.jar Eclipse
- IDE for Java Developers, Version: Helios Release, Build id: 20100617-1415
- log4j-1.2.6.jar
- junit-4.6.jar Eclipse
- 面向 Java 开发人员的 IDE,版本:Helios 发行版,构建 ID:20100617-1415
回答by Aaron Digulla
Look in the log4j.properties
or log4j.xml
file for the log level. It's probably set to ERROR
instead of DEBUG
在log4j.properties
orlog4j.xml
文件中查找日志级别。它可能设置为ERROR
而不是DEBUG
回答by Jon Freedman
One thing to note, if you have a log4j.properties file on your classpath you do not need to call BasicConfigurator. A description of how to configure the properties file is here.
需要注意的一件事是,如果您的类路径上有 log4j.properties 文件,则不需要调用 BasicConfigurator。关于如何配置属性文件的描述在这里。
You could pinpoint whether your IDE is causing the issue by trying to run this class from the command line with log4j.jar and log4j.properties on your classpath.
您可以通过尝试从命令行使用类路径上的 log4j.jar 和 log4j.properties 运行此类来确定您的 IDE 是否导致了问题。
回答by Tormod
Configuring with BasicConfigurator.configure();
sets up a basic console appender set at debug. A project with the setup above and no other code (except for a test) should produce three lines of logging in the console. I cannot say anything else than "it works for me".
使用配置BasicConfigurator.configure();
设置在调试时设置的基本控制台附加程序。具有上述设置且没有其他代码(测试除外)的项目应在控制台中生成三行日志记录。我只能说“它对我有用”。
Have you tried creating an empty project with just log4j and junit, with only the code above and ran it?
您是否尝试过仅使用 log4j 和 junit 创建一个空项目,并且仅使用上面的代码并运行它?
Also, in order to get the @Before
method running:
此外,为了让@Before
方法运行:
@Test
public void testname() throws Exception {
assertTrue(true);
}
EDIT:
编辑:
If you run more than one test at one time, each of them will call init before running.
如果一次运行多个测试,每个测试都会在运行前调用 init。
In this case, if you had two tests, the first would have one logger and the second test would call init again, making it log twice (try it) - you should get 9 lines of logging in console with two tests.
在这种情况下,如果您有两个测试,第一个将有一个记录器,第二个测试将再次调用 init,使其记录两次(尝试一下)-您应该在控制台中获得 9 行日志记录,并进行两次测试。
You might want to use a static init method annotated with @BeforeClass
to avoid this. Though this also happens across files, you might want to have a look at documentation on TestSuites in JUnit 4. And/or call BasicConfigurator.resetConfiguration();
in an @AfterClass annotated class, to remove all loggers after each test class / test suite.
您可能希望使用带有注释的静态 init 方法@BeforeClass
来避免这种情况。虽然这也会发生在文件中,但您可能希望查看 JUnit 4 中 TestSuites 的文档。和/或调用BasicConfigurator.resetConfiguration();
@AfterClass 注释类,以在每个测试类/测试套件之后删除所有记录器。
Also, the root logger is reused, so that if you set the root logger's level in a test method that runs early, it will keep this setting for all other tests that are run later, even if they are in different files. (will not happen when resetting configuration).
此外,根记录器被重用,因此如果您在早期运行的测试方法中设置根记录器的级别,它将为稍后运行的所有其他测试保留此设置,即使它们位于不同的文件中。(重置配置时不会发生)。
Testcase - this will cause 9 lines of logging:
测试用例 - 这将导致 9 行日志记录:
import static org.junit.Assert.assertTrue;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
public class SampleTest
{
private static final Logger LOGGER = Logger.getLogger(SampleTest.class);
@Before
public void init() throws Exception
{
// Log4J junit configuration.
BasicConfigurator.configure();
}
@Test
public void testOne() throws Exception {
LOGGER.info("INFO TEST");
LOGGER.debug("DEBUG TEST");
LOGGER.error("ERROR TEST");
assertTrue(true);
}
@Test
public void testTwo() throws Exception {
LOGGER.info("INFO TEST");
LOGGER.debug("DEBUG TEST");
LOGGER.error("ERROR TEST");
assertTrue(true);
}
}
Changing the init method reduces to the excepted six lines:
更改 init 方法减少到例外的六行:
@BeforeClass
public static void init() throws Exception
{
// Log4J junit configuration.
BasicConfigurator.configure();
}
Your problem is probably caused in some other test class or test suite where the logging level of the root logger is set to ERROR, and not reset.
您的问题可能是由其他一些测试类或测试套件引起的,其中根记录器的日志记录级别设置为 ERROR,而不是重置。
You could also test this out by resetting in the @BeforeClass method, before setting logging up.
在设置登录之前,您还可以通过在 @BeforeClass 方法中重置来测试这一点。
Be advised that these changes might break expected logging for other test cases until it is fixed at all places. I suggest trying out how this works in a separate workspace/project to get a feel for how it works.
请注意,这些更改可能会破坏其他测试用例的预期日志记录,直到它在所有地方得到修复。我建议在单独的工作区/项目中尝试它是如何工作的,以了解它是如何工作的。
回答by Bivas
Check that your log4j.properties
or log4j.xml
are copied to your IDE classpath and loads when calling BasicConfigurator.configure()
检查您的log4j.properties
或log4j.xml
是否已复制到您的 IDE 类路径并在调用时加载BasicConfigurator.configure()
回答by Thorbj?rn Ravn Andersen
Sounds like log4j picks up another configuration file than the one you think it does.
听起来 log4j 选择了另一个配置文件,而不是您认为的配置文件。
Put a breakpoint in log4j where the file is opened and have a look at the files getAbsolutePath().
在打开文件的 log4j 中放置一个断点并查看文件 getAbsolutePath()。
回答by Adriaan Koster
Check for log4j configuration files in your output (i.e. bin or target/classes) directory or within generated project artifacts (.jar/.war/.ear). If this is on your classpath it gets picked up by log4j.
检查输出(即 bin 或 target/classes)目录或生成的项目工件 (.jar/.war/.ear) 中的 log4j 配置文件。如果这是在您的类路径上,它会被 log4j 获取。
回答by Huzi--- Javiator
Go to Run configurations in your eclipse then -VM arguments add this: -Dlog4j.configuration=log4j-config_folder/log4j.xml
转到在 Eclipse 中运行配置,然后 -VM 参数添加以下内容: -Dlog4j.configuration=log4j-config_folder/log4j.xml
replace log4j-config_folder with your folder structure where you have your log4j.xml file
将 log4j-config_folder 替换为您拥有 log4j.xml 文件的文件夹结构
回答by Kiren
Thought this was a genuine defect but it turns out that my console size was limited, and caused the old content past 80000 characters to be cut off.
以为这是真正的缺陷,但事实证明我的控制台大小有限,导致超过 80000 个字符的旧内容被切断。
Right click on the console for the preferences and increase the console size limit.
右键单击控制台以获取首选项并增加控制台大小限制。
回答by Zsolt János
I once had an issue like this, when i downloadad a lib from Amazon (for Amazon webservices) and that jar file contained a log4j.properties and somehow that was used instead of my good old, self configed log4j. Worth a check.
我曾经遇到过这样的问题,当我从 Amazon(用于 Amazon webservices)下载一个库时,该 jar 文件包含一个 log4j.properties 并且以某种方式使用它而不是我的旧的、自我配置的 log4j。值得一试。