【问题标题】:How to add interceptor for spring boot logging in application.yml file如何在 application.yml 文件中添加用于 Spring Boot 日志记录的拦截器
【发布时间】:2022-10-17 18:22:31
【问题描述】:

我的项目有一个自定义的 HttpInterceptor 接口,可以实现它来拦截日志。

public interface HttpInterceptor {

 
    default String getName() {
        return getClass().getSimpleName();
    }

   
    default boolean isEnabled() {
        return Boolean.TRUE;
    }

   
    default boolean validate(HttpExchange exchange) {
        return Boolean.TRUE;
    }

  
    default HttpExchange postHandle(HttpExchange exchange) throws IOException {
        return exchange;
    }

    default HttpExchange afterCompletion(HttpExchange exchange) throws IOException {
        return exchange;
    }

    default HttpExchange preHandle(HttpExchange exchange) throws IOException {
        return exchange;
    }
}

现在,我有一个使用下面的代码实现上述拦截器的类。

public class Registrator implements HttpInterceptor {

   

    @Override
    public HttpExchange preHandle(HttpExchange exchange) throws IOException {        
         log.info("Getting intercepted for all Http calls");      

        return exchange;
    }

    @Override
    public HttpExchange afterCompletion(HttpExchange exchange) throws IOException {
        log.debug("Removing VIN from MDC");
      
    }
}

我正在努力让这个拦截器在我的应用程序中打印任何 HttpCalls 的日志。

应该在我的 application.yml 文件中添加什么让 springboot 知道使用此拦截器进行日志记录? 如何在 application.yml 中设置日志级别拦截器?

记录: 等级: ??

【问题讨论】:

    标签: java spring-boot logging https


    【解决方案1】:

    application.yml 中的日志级别可以这样设置

    logging.level.root=INFO
    logging.level.{yourpackage}.Registrator=INFO  // DEBUG, FATAL
    
    
    public class Registrator implements HttpInterceptor {
        private static final Logger logger = LoggerFactory.getLogger(Registrator.class);
    

    我正在使用 org.slf4j.Logger、org.slf4j.LoggerFactory;

    在 pom.xml 中

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.32</version>
        </dependency>
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多