开发者

Log4j only logs in main class. What I'm doing wrong? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I'm currently struggling to lo开发者_如何学运维g with log4j. I've created a xml configuration file and succeeded to log in my main class:

log4j.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="LogAppender" class="org.apache.log4j.FileAppender">
    <param name="file" value="logs/somelogfile.log" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n" />
    </layout>
  </appender>

  <root>
    <priority value="ERROR" />
    <appender-ref ref="LogAppender" />
  </root>

</log4j:configuration>

My main class:

public class Main {

    static Logger logger = Logger.getLogger(Main.class);
    /**
     * @param args
     */

    public static void main(String[] args) {
            // TODO Auto-generated method stub
        DOMConfigurator.configure("config/log4j.xml");

        logger.error("Hi");
    }

}

Unfortunatly, logging in other classes (in another package) doens't work. No matter if I call the DOMConfigurator or not, nothing is beeing logged. What I'm missing here? (Logger is always created with Logger.getLogger(Classname.class))

EDIT:

Sorry, I'm a dump! It's working. Just recognised, that I was catching the wrong exception in my other class, so that the logger was never called.


You need to provision loggers in your log4j.xml file to point to appenders. Here is an example:

<logger name="com.mycompany.apackage.MyClass">
  <level value="info"/>
  <appender-ref ref="appender-name-here" />
</logger>

Also, your root logger will only allow ERROR level logging (priority value="ERROR").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜