gpt4 book ai didi

java - debug 未保存在 debug.log 下

转载 作者:行者123 更新时间:2023-12-02 05:02:42 26 4
gpt4 key购买 nike

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

关于java - debug 未保存在 debug.log 下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28093247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com