Log4j output not displayed in Eclipse console

2019-01-22 02:29发布

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.

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?

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

17条回答
成全新的幸福
2楼-- · 2019-01-22 03:28

Sounds like log4j picks up another configuration file than the one you think it does.

Put a breakpoint in log4j where the file is opened and have a look at the files getAbsolutePath().

查看更多
霸刀☆藐视天下
3楼-- · 2019-01-22 03:29

Look in the log4j.properties or log4j.xml file for the log level. It's probably set to ERROR instead of DEBUG

查看更多
家丑人穷心不美
4楼-- · 2019-01-22 03:32

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.

查看更多
Viruses.
5楼-- · 2019-01-22 03:32

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.

查看更多
女痞
6楼-- · 2019-01-22 03:32

The Eclipse Console Window has a Filter Property (in some cases only system.err is active).

Rigt click into Eclipse console window -> Preferences -> Console and ensure that checkbox

Show when Program writes to standard out

is active.

Alternatively you can configure log4j to write alway to System.err like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="AppConsole" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.err" />
        <param name="Encoding" value="ISO-8859-15" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{dd.MM.yyyy HH:mm:ss} %5p kontext.%c{1}:%L - %m%n" />
        </layout>
    </appender>
    <root>
        <level value="debug" />
        <appender-ref ref="AppConsole" />
    </root>
</log4j:configuration> 
查看更多
登录 后发表回答