SpringBoot2 中如何使用 Log4j2 动态记录日志并根据 API 接口路径保存到对应路径的文件?

springboot2 中如何使用 log4j2 动态记录日志并根据 api 接口路径保存到对应路径的文件?

springboot2 log4j2 如何动态记录日志,并将日志根据api接口路径,保存到对应路径的文件中?

这个问题可以通过使用 log4j2 的 routingappender 来实现。routingappender 允许根据指定条件将日志路由到不同的目的地。

步骤:

  1. 在 log4j2-spring.xml 中配置 routingappender:
<configuration status="debug" monitorinterval="5"><appenders><routing name="routingappender"><routes pattern="${ctx:routingkey}"><route key="special"><rollingfile name="rolling-${ctx:routingkey}" filename="logs/special-${ctx:routingkey}.log" filepattern="logs/${date:yyyy-mm}/${ctx:routingkey}-other-%d{yyyy-mm-dd}-%i.log.gz"><patternlayout><pattern>%d{iso8601} [%t] %p %c{3} - %m%n</pattern></patternlayout><policies><timebasedtriggeringpolicy interval="6" modulate="true"></timebasedtriggeringpolicy><sizebasedtriggeringpolicy size="10 mb"></sizebasedtriggeringpolicy></policies></rollingfile></route><route key="default"><rollingfile name="rolling-default" filename="logs/default.log" filepattern="logs/${date:yyyy-mm}/default-%d{yyyy-mm-dd}-%i.log.gz"><patternlayout><pattern>%d{iso8601} [%t] %p %c{3} - %m%n</pattern></patternlayout><policies><timebasedtriggeringpolicy interval="6" modulate="true"></timebasedtriggeringpolicy><sizebasedtriggeringpolicy size="10 mb"></sizebasedtriggeringpolicy></policies></rollingfile></route><route><rollingfile name="rolling-${ctx:routingkey}" filename="logs/${ctx:routingkey}/${date:yyyy-mm}.log" filepattern="logs/${date:yyyy-mm}/${ctx:routingkey}-other-%d{yyyy-mm-dd}-%i.log.gz"><patternlayout><pattern>%d{iso8601} [%t] %p %c{3} - %m%n</pattern></patternlayout><policies><timebasedtriggeringpolicy interval="6" modulate="true"></timebasedtriggeringpolicy><sizebasedtriggeringpolicy size="10 mb"></sizebasedtriggeringpolicy></policies></rollingfile></route></routes></routing></appenders><loggers><root level="info"><appenderref ref="routingappender"></appenderref></root></loggers></configuration>
  1. 创建一个拦截器类,在请求处理前将接口路径动态写入到 threadcontext 中:
public class log4j2interceptor implements handlerinterceptor {
    
    @override
    public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {
        
        string routingkey = request.getrequesturi().substring(request.getcontextpath().length());
        threadcontext.put("routingkey", routingkey);
        
        return true;
    }
    
    @override
    public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception {
        threadcontext.remove("routingkey");
    }
    
}
  1. 注册拦截器:
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration log4j2Registration = registry.addInterceptor(useLog4j2Inter());
                log4j2Registration
                    .addPathPatterns("/**");
    }

    @Bean
    public Log4j2Interceptor useLog4j2Inter() {
        return new Log4j2Interceptor();
    }
}

以上就是SpringBoot2 中如何使用 Log4j2 动态记录日志并根据 API 接口路径保存到对应路径的文件?的详细内容,更多请关注其它相关文章!