问题
My log4j logger does not want to use the log4j.xml file to be configured. This file is located in the src folder and looks like that:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="ROLLING" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:\debug\myproject\logfile.log" />
<param name="MaxFileSize" value="10024KB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L) -%m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ROLLING" />
</root>
</log4j:configuration>
But logfile.log is still empty and there is no "DEBUG" line in the console.
NB: it is a Java EE project on JBoss 7.1.0 and using Struts2.
回答1:
The following answer is not entirely mine. It was posted in the question itself by the OP, so I'm moving it into this community wiki answer.
I resolved it by explicitely configuring the logging configuration.
Note that you won't be able to change the configuration at runtime if you configure it using a file.
For more information, see: https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F
回答2:
Make sure the log4j.xml file is located in the /WEB-INF/classes directory of your web application.
回答3:
The problem was with the log file. If you use the absolute file name make sure to replace backslashes with slash. For example
<param name="File" value="C:/debug/myproject/logfile.log" />
来源:https://stackoverflow.com/questions/15026408/log4j-configuration-not-used