我们在项目中使用 log4j JDBCAppender。但是,它无法将异常堆栈跟踪记录到数据库。我在网上搜索,我得到了 %throwable,但这不适用于 JDBCAppender。有人可以帮我做这件事吗?
这是我的自定义 Appender 代码,我将其与 JDBCAppender 相同,并稍作修改
public void append(LoggingEvent event) {
ArrayList buffer = new ArrayList();
buffer.add(event);
flushBuffer(buffer);
}
public void flushBuffer(ArrayList buffer) {
for (Iterator i = buffer.iterator(); i.hasNext();) {
try {
LoggingEvent logEvent = (LoggingEvent) i.next();
String messageRecievied = getLogStatement(logEvent);
execute(messageRecievied);
} catch (SQLException e) {
this.errorHandler.error("Failed to excute sql", e, 2);
}
}
}
我不知道为什么它不包括堆栈跟踪
这是我的 log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender class="com.MyAppender" name="customAppen">
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param value="%d[%t] %-5p(%F:<%M>:%L)- %m%n" name="ConversionPattern"/>
</layout>
</appender>
<appender class="org.apache.log4j.AsyncAppender" name="asynchapp">
<param name="Blocking" value="false"/>
<appender-ref ref="customAppen"/>
</appender>
<root>
<level value = "INFO"/>
<appender-ref ref="asynchapp"/>
</root>