携程Apollo动态配置日志级别

在application.properties中配置 logging.level.com.umgsai.core.dal=debug 可以在日志中打印出详细的SQL语句。

 

但是并不希望在线上环境也打印,此时需要在线上和线下配置不同的日志级别。

@Slf4j
@Service
public class DynamicLoggersConfig {

    private static final String DAL_PACKAGE = "com.umgsai.core.dal";
    @Value("${logging.level.com.umgsai.core.dal:info}")
    private String dalLogLevel = "info";

    @ApolloConfig
    private Config config;
    private final static String LoggerTag = "logging.level.";
    private final LoggingSystem loggingSystem;

    public DynamicLoggersConfig(LoggingSystem loggingSystem) {
        Assert.notNull(loggingSystem, "LoggingSystem must not be null");
        this.loggingSystem = loggingSystem;
    }

    @ApolloConfigChangeListener
    private void configChangeListener(ConfigChangeEvent changeEvent) {
        Set<String> keyNames = config.getPropertyNames();
        for (String key : keyNames) {
            if (StringUtils.containsIgnoreCase(key, LoggerTag)) {
                String strLevel = config.getProperty(key, "info");
                LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
                loggingSystem.setLogLevel(key.replace(LoggerTag, ""), level);
            }
            log.info("{}:{}", key, config.getProperty(key, null));
        }
    }

    @PostConstruct
    private void setLogLevel() {
        LogLevel level = LogLevel.valueOf(dalLogLevel.toUpperCase());
        loggingSystem.setLogLevel(DAL_PACKAGE, level);
        log.info("{}:{}", DAL_PACKAGE, dalLogLevel);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值