jboss中如何收集与单个请求相关的日志?
我正在开发一个在 JBoss 下运行的 Java EE Web 应用程序。
我想做以下事情: 当用户发送http请求(通过打开页面或通过AJAX)时,与该请求相关的所有日志都会被收集,然后保存到数据库中。 我所说的相关是指它们在处理当前请求的过程中被记录。 最困难的部分是收集与单个请求相关的日志。
我正在研究这个解决方案:
JBoss 使用 log4j 进行日志记录。 当应用程序启动时,启动侦听器会注册一个 log4j 附加程序,该附加程序将所有日志收集到 ThreadLocal 字段中。 在请求处理结束时,从现场获取日志并将其保存到数据库中。
但是,现在看来,log4j 附加程序在其他线程中工作。 这使得该解决方案不可能实现。
你知道如何做到这一点吗?
谢谢, 阿尔乔姆 B.
I am developing a Java EE web application that is run under JBoss.
I want to do the following:
When a user sends an http request (by opening a page or through AJAX) all the logs that are related to this request are collected and then saved into the database. By related I mean that they are being logged during the process of handling the current request. Tha hardest part is collecting the logs related to a single request.
I was looking into this solution:
JBoss uses log4j for logging. When the app starts, a start listener registers a log4j appender that collects all the logs into a ThreadLocal field. In the end of the request handling the logs are taken from the field and saved into the DB.
But, it seems now, that log4j appenders work in other threads. This makes this solution imposible.
Do you have any idea, how this could be made?
Thanks,
Artem B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 log4j MDC 类(映射诊断上下文)将某些数据与当前线程关联起来。
我经常使用它来将会话 ID 添加到日志输出中,以获取该会话的任何日志记录:
然后,您只需在 PatternLayout 中通过键引用 MDC。 不确定 DB 附加程序如何工作,但我认为它也可以记录 MDC 字段......
You can use the log4j MDC class (Mapped Diagnostic Context) to associate certain data with the current thread.
I use this often to add the Session ID to the log output for whatever logging is going in for that session:
Then you just refer to the MDC by key in your PatternLayout. Not sure how the DB appender works, but I assume it can log MDC fields too...