我使用x4配置使用log4j创建了一个应用程序,应用程序根据级别保存日志,将错误日志保存到error.log,将信息日志保存到info.log。
应用程序运行正常但调试日志未在debug.log文件下保存。休息所有日志都正确保存
任何人都可以告诉我一些解决方案吗
我的 log4jConfig.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="debug-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${openfireHome}/logs/debug.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-7p %10c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="debug" />
<param name="LevelMin" value="debug" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="info-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${openfireHome}/logs/info.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-7p %10c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="info" />
<param name="LevelMin" value="info" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="warn-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${openfireHome}/logs/warn.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-7p %10c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="warn" />
<param name="LevelMin" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="error-out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${openfireHome}/logs/error.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-7p %10c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="error" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<root>
<level value="info" />
<appender-ref ref="debug-out" />
<appender-ref ref="info-out" />
<appender-ref ref="warn-out" />
<appender-ref ref="error-out" />
</root>
</log4j:configuration>
StartupListener.java
package com.log4jdemo;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class StartupListener implements ServletContextListener
{
@Override
public void contextInitialized(ServletContextEvent event)
{
ServletContext servletContext = event.getServletContext();
System.setProperty("openfireHome", servletContext.getRealPath("/"));
Logger logger = null;
String log4jFile = servletContext.getInitParameter("log4jConfig");
System.out.println(log4jFile);
DOMConfigurator.configure(log4jFile);
logger = LogManager.getLogger(StartupListener.class.getName());
logger.debug("Loaded: " + log4jFile);
logger.info("This is a logging statement from log4j");
logger.debug("Show DEBUG message");
logger.info("Show INFO message");
logger.warn("Show WARN message");
logger.error("Show ERROR message");
logger.fatal("Show FATAL message");
System.out.println(servletContext.getRealPath("/")+"/WEB-INF/logs/");
}
@Override
public void contextDestroyed(ServletContextEvent arg0)
{
}
}
答案 0 :(得分:1)
这是因为你的根级别:
<root>
<level value="info" />
<appender-ref ref="debug-out" />
<appender-ref ref="info-out" />
<appender-ref ref="warn-out" />
<appender-ref ref="error-out" />
</root>
您需要将级别值设置为&#34; debug&#34;
DEBUG&lt;信息&lt;警告&lt; ERROR
DEBUG是四个中最详细的
由于您已将其保留在INFO,因此下面的所有日志级别都正确打印。
答案 1 :(得分:1)
您需要更改根记录器级别。如果您需要所有级别,可以输入以下内容::
<root>
<level value="all" />
<appender-ref ref="debug-out" />
<appender-ref ref="info-out" />
<appender-ref ref="warn-out" />
<appender-ref ref="error-out" />
</root>
以下是log4j中可用的级别:
Level
-----
ALL # Everything is written.
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
OFF # Nothing is written.
ALL
和OFF
是特殊级别。
如果您要求所有级别的一个日志,请添加未过滤且没有阈值的appender。 e.g:
<appender name="out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${openfireHome}/logs/out.log" />
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-7p %10c{1} - %m%n" />
</layout>
</appender>
在根记录器中:
<root>
<level value="all" />
<appender-ref ref="out" />
<appender-ref ref="debug-out" />
<appender-ref ref="info-out" />
<appender-ref ref="warn-out" />
<appender-ref ref="error-out" />
</root>