调试未保存在 debug.log 下

debug is not saving under debug.log

我使用 log4j 使用 xml 配置创建了一个应用程序,应用程序根据级别保存日志,..ierror 日志到 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)
    {

    }
}

因为你的root级别:

<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>

您需要将级别值设置为"debug"

调试 < 信息 < 警告 < 错误

DEBUG 是四个中最详细的

由于您将其保存在 INFO,因此所有低于该级别的日志级别都可以正确打印。

您需要更改根记录器级别。如果你需要 all 级别,你可以输入以下内容::

<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.

ALLOFF是特殊级别。

如果您需要一个包含所有级别的日志,请添加一个未过滤且没有阈值的附加程序。例如:

<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>