这是我的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
。我想获得更多关于此的信息。我阅读了有关启用弹簧调试和遵循说明的内容,但它不起作用。我检查了我的请求,似乎完全没问题。如何在server.log文件中查看有关此内容的更多信息?为什么上面的配置不起作用?
PS:为了清楚起见,我删除了其他记录器和追加器
答案 0 :(得分:0)
这解决了它
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- Rolling file Appender -->
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.base}/logs/server.log" />
<param name="Append" value="true" />
<!-- changed to DEBUG-->
<param name="Threshold" value="DEBUG" />
<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>
<!-- added this-->
<category name="org.springframework.beans">
<priority value="debug" />
</category>
答案 1 :(得分:0)
这里是依赖项:
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的日志路径。
<?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.dmoney" 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
是日志文件的名称。
<Property name="logPatternConsole">
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %highlight{%-5level} %logger{35} - %msg%n{INFO=cyan, ERROR=red}
</Property>
在这里,logPatternConsole
具有时间模式。
在“ Appender”部分中,它具有
<Policies>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
此处SizeBasedTriggeringPolicy
定义何时创建新的日志文件。在这里, 10MB 之后,它将创建新的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();
}
}
谢谢:)