将上下文路径添加到 SLF4J 日志语句 [英] Adding context path to SLF4J log statements

查看:41
本文介绍了将上下文路径添加到 SLF4J 日志语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单个 tomcat 中部署了多个 Spring webapp,并将错误消息记录在单个日志文件中.我需要记录请求被命中的 web 应用程序的上下文路径.目前我必须实现一个 servlet 过滤器,以便为每个请求设置变量值.这必须完成,因为 MDC 实例是每个线程的.

I have multiple Spring webapps deployed in single tomcat, and logging the error messages in single log file. I need to log the context path of the webapp to which the request was hit. Currently I have to implement a servlet filter so that the variable value is set for each request. This has to be done because MDC instance is per thread.

由于应用程序的上下文路径是恒定的,有没有办法只设置一次 MDC 变量?

log4j.appender.LOGFILE.layout.ConversionPattern=%d %X{applicationId} %-5p %c - %m%n 

为了在 MDC 中设置 applicationId 我已经定义了一个 Servlet 过滤器

To set the applicationId in MDC I have defined a Servlet Filter

public class LogFilter implements Filter {

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    MDC.put("applicationId", request.getServletContext().getContextPath());
    chain.doFilter(request, response);
  }
}

推荐答案

没找到其他方法,所以问题中提到的代码似乎是唯一的选择.关闭线程.

Didn't find any other way, so the code mentioned in the question seems to be the only option. Closing the thread.

这篇关于将上下文路径添加到 SLF4J 日志语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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