jboss中如何收集与单个请求相关的日志?

发布于 2024-07-17 04:12:23 字数 425 浏览 2 评论 0原文

我正在开发一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-07-24 04:12:23

您可以使用 log4j MDC 类(映射诊断上下文)将某些数据与当前线程关联起来。

我经常使用它来将会话 ID 添加到日志输出中,以获取该会话的任何日志记录:

public class AddSessionIdToLogFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
            ServletException {

            if (request instanceof HttpServletRequest) {
                HttpServletRequest httpRequest = (HttpServletRequest) request;
                String sessionID = httpRequest.getSession().getId();

                MDC.put("SessionID", sessionID);
            }   

            ...

然后,您只需在 PatternLayout 中通过键引用 MDC。 不确定 DB 附加程序如何工作,但我认为它也可以记录 MDC 字段......

log4j.appender.LOGFILE.layout.ConversionPattern= ... [SessionID=%X{SessionID}] ...

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:

public class AddSessionIdToLogFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
            ServletException {

            if (request instanceof HttpServletRequest) {
                HttpServletRequest httpRequest = (HttpServletRequest) request;
                String sessionID = httpRequest.getSession().getId();

                MDC.put("SessionID", sessionID);
            }   

            ...

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...

log4j.appender.LOGFILE.layout.ConversionPattern= ... [SessionID=%X{SessionID}] ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文