0. 问题来源
https://blog.youkuaiyun.com/a116475939/article/details/88526218
继上一篇博客之后,已经可以在发布cxf接口,并且记录日志报文到日志文件中。
但是所有接口的日志都在一个文件中,这样很不合理,不便于追溯问题查找日志。
所以今天则要实现这个功能。
1. 问题思考
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
LoggingFeature logFeature = new LoggingFeature();
logFeature.setPrettyLogging(true);
logFeature.initialize(springBus);
springBus.getFeatures().add(logFeature);
return springBus;
}
在上个博客中,笔者使用LoggingFeature作为默认的日志记录工具。
然而,进入LoggingFeature之后,发现这个类已经弃用了。
import org.apache.cxf.common.injection.NoJSR250Annotations;
import org.apache.cxf.interceptor.AbstractLoggingInterceptor;
import org.apache.cxf.interceptor.InterceptorProvider;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
@NoJSR250Annotations
@Deprecated
@Provider(value = Type.Feature)
*
* @deprecated use the logging module rt/features/logging instead
*/
public class LoggingFeature extends AbstractFeature {
private static final int DEFAULT_LIMIT = AbstractLoggingInterceptor.DEFAULT_LIMIT;
private static final LoggingInInterceptor IN = new LoggingInInterceptor(DEFAULT_LIMIT);
通过,这句话,了解到已经这块东西应该是废弃 并移植到另外的模块里了
use the logging module rt/features/logging instead
于是我就找到了
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
<version>3.2.5</version>
</dependency>
然后查找资料,配置springBus,添加日志拦截器LoggingInInterceptor/LoggingOutInterceptor
/**
* @description 启用日志记录
* @author fg
* @date 2019年1月11日
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
LoggingInInterceptor ipt = new LoggingInInterceptor();
LoggingOutInterceptor opt = new LoggingOutInterceptor();
ipt.setPrettyLogging(true);
ipt.setLimit(LOG_LIMIT);
opt.setPrettyLogging(true);
opt.setLimit(LOG_LIMIT);
springBus.getInInterceptors().add(ipt);
springBus.getOutInterceptors().add(opt);
return springBus;
}
然而日志并未及时生效。
2. 深入分析
通过查看LoggingInInterceptor源码
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agr