Log4j only logs in main class. What I'm doing wrong? [closed]
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").
精彩评论