问题描述
这是我的log4j.xml
This is my log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/server.log" />
<param name="Append" value="true" />
<param name="Threshold" value="INFO" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE}#%X{requestId}#%X{uid}#%X{agentId}#%X{agentName} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<logger name="org.springframework" level="DEBUG">
<appender-ref ref="FILE"/>
</logger>
我正在向控制器发出JSON请求并获得400 Bad request request is syntactically incorrect
.我想获得更多有关此的信息.我阅读了有关启用Spring调试的信息,并按照说明进行了操作,但这没有用.我已经检查了我的要求,看来完全没问题.如何在server.log文件中看到有关此信息的更多信息?为什么上述配置不起作用?PS:为清楚起见,我删除了其他记录器和附加器
I am making a JSON request to a controller and getting 400 Bad request request is syntactically incorrect
. I want to get some more info on this. I read about enabling spring debugging and followed instructions but it doesn't work. I have checked my request and it seems completely fine. How can I see more information about this in server.log file? Why does the above configuration not work?PS: I have removed the other loggers and appenders for clarity
推荐答案
以下是依赖项:
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'
这是log4j2配置:
以下配置适用于Windows计算机.您只能更改linux的日志路径.
Bellow configuration is for windows machine. You can change only the log path for linux.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" strict="true">
<Properties>
<Property name="filename">D:/CommonLogFile/YOUR_LOG_FILE_NAME.log
</Property>
<Property name="backupFilePattern">
D:/CommonLogFile/PROJECT_NAME-%d{yyyy-MM-dd HH_mm}-%i.log
</Property>
<Property name="logPatternConsole">
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight{%-5level} %logger{35} - %msg%n{INFO=cyan, ERROR=red}
</Property>
<Property name="logPatternFile">
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg%n
</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${logPatternConsole}"/>
</Console>
<RollingFile name="MyRollingFile" fileName="${filename}" filePattern="${backupFilePattern}">
<PatternLayout pattern="${logPatternFile}"/>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.project" level="trace" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="MyRollingFile"/>
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
此处包含一些关键提示:
<Property name="filename">D:/CommonLogFile/YOUR_LOG_NAME.log
</Property>
在这里,filename
是日志文件的名称.
Here, filename
is the name of the log file.
<Property name="logPatternConsole">
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight{%-5level} %logger{35} - %msg%n{INFO=cyan, ERROR=red}
</Property>
在这里,logPatternConsole
具有时间模式.
Here, logPatternConsole
has the time pattern.
在Appender部分中,它具有
In Appender section, it has
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
此处SizeBasedTriggeringPolicy
定义何时创建新的日志文件.在这里, 10MB 之后,它将创建新的logFile
here SizeBasedTriggeringPolicy
defins when new log file will created. Here, after 10MB it will create new logFile
以下是日志实现:
static LogWriterUtility logWriterUtility = new LogWriterUtility(YOUR_CLASS_NAME.class);
logWriterUtility.trace(UUID.randomUUID().toString(),"Here is the log message");
检查波纹管是否为LogWriterUtility
类:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LogWriterUtility {
Logger log;
public LogWriterUtility(Class<?> clazz) {
log = LogManager.getLogger(clazz);
}
public void trace(String requestId, String message) {
log.trace("REQUESTID:" + requestId + ":" + message);
}
public void debug(String requestId, String message) {
log.debug("REQUESTID:" + requestId + ":" + message);
}
public void error(String requestId, String message) {
log.error("REQUESTID:" + requestId + ":" + message);
}
public void error(String requestId, Throwable t) {
log.error("REQUESTID:" + requestId + ":");
log.error(t);
}
public void info(String requestId, String message) {
log.info("REQUESTID:" + requestId + ":" + message);
}
public void error(String requestId, Exception exception) {
log.warn("REQUESTID:" + requestId + ":" + exception.getStackTrace());
}
public void errorWithAnalysis(String requestId, Exception exception) {
if(exception==null)
return;
String message="No Message on error";
StackTraceElement[] stackTrace = exception.getStackTrace();
if(stackTrace!=null && stackTrace.length>0) {
message="";
for (StackTraceElement e : stackTrace) {
message += "\n" + e.toString();
}
}
log.error("REQUESTID:" + requestId + ":" +message);
}
public void warn(String requestId, String message) {
log.error(requestId,message);
}
public boolean isTraceEnabled() {
return log.isTraceEnabled();
}
}
谢谢:)
这篇关于无法使用log4j启用Spring调试日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!