Log4j JDBCAppender记录堆栈跟踪 [英] Log4j JDBCAppender to log stacktraces

查看:144
本文介绍了Log4j JDBCAppender记录堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用org.apache.log4j.jdbc.JDBCAppender,如何将用warnerror记录的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依赖项

将其包含在pom中:

<dependency>
  <groupId>log4j</groupId>
  <artifactId>apache-log4j-extras</artifactId>
  <version>1.1</version>
</dependency>

您现在可以访问%throwable

You now have access to %throwable

%throwable{short}%throwable{1}将输出堆栈的第一行 痕迹. throwable{none}throwable{0}将取消堆栈跟踪. 如果正整数,%throwable{n}将输出n行堆栈跟踪 或如果为负整数,则省略最后-n行.

%throwable{short} or %throwable{1} will output the first line of stack trace. throwable{none} or throwable{0} will suppress the stack trace. %throwable{n} will output n lines of stack trace if a positive integer or omit the last -n lines if a negative integer.

我已添加到表中,

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记录堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆