springboot2自定義HttpTraceRepository

本文主要研究下springboot2自定義HttpTraceRepositoryhtml

Configuration

@Configuration
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = "management.trace.http", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(HttpTraceProperties.class)
@AutoConfigureBefore(HttpTraceAutoConfiguration.class)
public class TraceConfig {

    @Bean
    @ConditionalOnMissingBean(HttpTraceRepository.class)
    public RemoteHttpTraceRepository traceRepository() {
        return new RemoteHttpTraceRepository();
    }
}

這裏新增了一個配置,在HttpTraceAutoConfiguration以前,自定義了traceRepositoryweb

RemoteHttpTraceRepository

public class RemoteHttpTraceRepository implements HttpTraceRepository {

    private static final Logger LOGGER = LoggerFactory.getLogger(RemoteHttpTraceRepository.class);

    @Override
    public List<HttpTrace> findAll() {
        return Collections.emptyList();
    }

    @Override
    public void add(HttpTrace trace) {
        //send log to remote server or log center
        LOGGER.info(JSON.toJSONString(trace));
    }
}

自定義RemoteHttpTraceRepository,能夠本身在add方法將trace輸出到日誌服務器spring

小結

經過自定義HttpTraceRepository,對於webflux的工程來講能夠本身實現相似tomcat的accesslog,很是方便。tomcat

doc

相關文章
相關標籤/搜索