Skip to content

Add "LoggingFactoryBeanCustomizers" #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -38,6 +40,7 @@
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

import static org.minbox.framework.api.boot.autoconfigure.logging.ApiBootLoggingProperties.API_BOOT_LOGGING_PREFIX;

Expand Down Expand Up @@ -74,18 +77,24 @@ public ApiBootLoggingAutoConfiguration(ApiBootLoggingProperties apiBootLoggingPr
this.apiBootLoggingProperties = apiBootLoggingProperties;
}

@Bean
@ConditionalOnMissingBean
public LoggingFactoryBeanCustomizers loggingFactoryBeanCustomizers(ObjectProvider<LoggingFactoryBeanCustomizer> customizers) {
return new LoggingFactoryBeanCustomizers(customizers.orderedStream().collect(Collectors.toList()));
}

/**
* logging factory bean
* {@link LoggingFactoryBean}
*
* @param loggingAdminDiscoveryObjectProvider Logging Admin Discovery Instance Provider
* @param customizerObjectProvider LoggingFactory Bean Customizer
* @param customizers LoggingFactory Bean Customizers
* @return LoggingFactoryBean
*/
@Bean
@ConditionalOnMissingBean
public LoggingFactoryBean loggingFactoryBean(ObjectProvider<LoggingAdminDiscovery> loggingAdminDiscoveryObjectProvider,
ObjectProvider<List<LoggingFactoryBeanCustomizer>> customizerObjectProvider) {
LoggingFactoryBeanCustomizers customizers) {
LoggingFactoryBean factoryBean = new LoggingFactoryBean();
factoryBean.setIgnorePaths(apiBootLoggingProperties.getIgnorePaths());
factoryBean.setIgnoreHttpStatus(apiBootLoggingProperties.getIgnoreHttpStatus());
Expand All @@ -97,12 +106,8 @@ public LoggingFactoryBean loggingFactoryBean(ObjectProvider<LoggingAdminDiscover
factoryBean.setShowConsoleLog(apiBootLoggingProperties.isShowConsoleLog());
factoryBean.setFormatConsoleLog(apiBootLoggingProperties.isFormatConsoleLogJson());

List<LoggingFactoryBeanCustomizer> customizers = customizerObjectProvider.getIfAvailable();
if (!ObjectUtils.isEmpty(customizers)) {
customizers.stream().forEach(customizer -> customizer.customize(factoryBean));
}
logger.info("【LoggingFactoryBean】init successfully.");
return factoryBean;
return customizers.customize(factoryBean);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.minbox.framework.api.boot.autoconfigure.logging;

import org.minbox.framework.logging.client.LoggingFactoryBean;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers;
import org.springframework.boot.util.LambdaSafe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* The {@link LoggingFactoryBeanCustomizer} collection processing class
*
* @author 恒宇少年
*/
public class LoggingFactoryBeanCustomizers {
private List<LoggingFactoryBeanCustomizer> customizers;

public LoggingFactoryBeanCustomizers(List<LoggingFactoryBeanCustomizer> customizers) {
this.customizers = (customizers != null) ? new ArrayList<>(customizers) : Collections.emptyList();
}

/**
* Customize the specified {@link LoggingFactoryBean}. Locates all
* {@link LoggingFactoryBeanCustomizer} beans able to handle the specified instance and
* invoke {@link LoggingFactoryBeanCustomizer#customize(LoggingFactoryBean)} on them.
*
* @param factoryBean the logging factory bean to customize
* @return the factory bean
*/
public LoggingFactoryBean customize(LoggingFactoryBean factoryBean) {
LambdaSafe.callbacks(LoggingFactoryBeanCustomizer.class, this.customizers, factoryBean)
.withLogger(LoggingFactoryBeanCustomizer.class).invoke((customizer) -> customizer.customize(factoryBean));
return factoryBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.minbox.framework.api.boot.sample.logging;

import org.minbox.framework.api.boot.autoconfigure.logging.LoggingFactoryBeanCustomizer;
import org.minbox.framework.logging.client.LoggingFactoryBean;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* {@link LoggingFactoryBeanCustomizer}实现类
* 新增排除日志拦截输出的路径
*
* @author 恒宇少年
*/
@Component
@Order(2)
public class AppendIgnorePathCustomizer implements LoggingFactoryBeanCustomizer {
@Override
public void customize(LoggingFactoryBean factoryBean) {
factoryBean.getIgnorePaths().add("/test");
}
}