问题描述
使用org.apache.log4j.jdbc.JDBCAppender
,如何将用warn
和error
记录的stracktrace记录到PatternLayout
中.
Using org.apache.log4j.jdbc.JDBCAppender
, how can I get stracktraces logged with warn
and error
into the PatternLayout
.
我的记录方式是
logger.warn("warning description", e);
logger.error("error description", e);
我将String描述放入表中,但是Throwable的stacktrace现在位于此处.是否可以通过PatternLayout
访问另一个参数.目前我正在使用
I get the String descriptions into the table, but the Throwable's stacktrace is now where. Is there another parameter that I can access via the PatternLayout
. Currently I am using
"INSERT INTO app_logs (app, log_date, log_level, location, loc, message) VALUES ('my-apps-name', '%d{ISO8601}','%p', '%C.java', '%C{1}.java:%L', '%m')"
插入表格
TABLE `app_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app` varchar(255) DEFAULT NULL,
`log_date` varchar(255) DEFAULT NULL,
`log_level` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`loc` varchar(255) DEFAULT NULL,
`message` text,
PRIMARY KEY (`id`)
)
推荐答案
我找到了解决方案.
将PatternLayout
类替换为EnhancedPatternLayout
类.
org.apache.log4j.EnhancedPatternLayout
您还需要包含 apache-log4j-extra依赖项
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.1</version>
</dependency>
您现在可以访问%throwable
You now have access to %throwable
我已添加到表中,
TABLE `app_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app` varchar(255) DEFAULT NULL,
`log_date` varchar(255) DEFAULT NULL,
`log_level` varchar(255) DEFAULT NULL,
`location` varchar(255) DEFAULT NULL,
`loc` varchar(255) DEFAULT NULL,
`message` text,
`throwable` text,
`stacktrace` text,
PRIMARY KEY (`id`)
)
并更新了我的模式以填充这些列.
And updated my pattern to populate these columns.
"INSERT INTO app_logs (app, log_date, log_level, location, loc, message, throwable, stacktrace) VALUES ('my-apps-name', '%d{ISO8601}','%p', '%C.java', '%C{1}.java:%L', '%m', '%throwable{short}', '%throwable{100}')"
这篇关于Log4j JDBCAppender记录堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!