结合apollo动态刷新每个类的日志级别

结合apollo动态刷新每个类的日志级别

背景

  • 在线上环境,有时日志输出太多,日志文件过大。为了控制日志文件的大小,需要删除日志输出语句,或者修改日志级别。但修改之后,需求重启服务。
  • 为了实现不重启服务的情况下,可以修改日志级别,来控制日志文件的大小,可以借助于Apollo来动态刷新日志级别。
  • 这里显示的是控制每个类的日志级别

修改application.yml配置

  • 重点是增加如下两个配置:

    apollo.bootstrap.enabled=true
    #开启后,Apollo的配置会先于logback的配置加载
    apollo.bootstrap.eagerLoad.enabled=true
    
  • 修改之后的【applicaiton.yml】配置。

    apollo:
      bootstrap:
        enabled: true
        #开启后,Apollo的配置会先于logback的配置加载
        eagerLoad:
          enabled: true
        # 配置命名空间
        namespaces: application.yml,abc.properties
      # 本地环境
      meta: http://localhost:18080
    app:
      id: abcService
    

增加日志级别监听配置类

  • 在代码中增加如下的代码

    package com.evan.config;
    
    import com.ctrip.framework.apollo.Config;
    import com.ctrip.framework.apollo.model.ConfigChangeEvent;
    import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
    import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.logging.LogLevel;
    import org.springframework.boot.logging.LoggingSystem;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.env.Environment;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.PostConstruct;
    import java.util.Set;
    
    /**
     * @description: 【结合apollo动态刷新日志级别】
     * @author: Evan
     * @time: 2021/9/6 9:58
     */
    @Service
    public class LogListenerConfig implements ApplicationContextAware {
    
        @Autowired
        private Environment env;
    
        private ApplicationContext applicationContext;
    
        private static final Logger LOGGER = LogManager.getLogger(LogListenerConfig.class);
    
        private static final String LOGGER_TAG = "logging.level.";
    
        @Autowired
        private LoggingSystem loggingSystem;
    
        @ApolloConfig
        private Config config;
    
        @PostConstruct
        private void initialize() {
        }
    
        /**
         * 配置监听
         * ApolloConfigChangeListener > value 属性默认 命名空间 "application"
         *
         * 示例: @ApolloConfigChangeListener(value = {"application", "test"})
         */
        @ApolloConfigChangeListener(value = {"application"})
        private void onChange(ConfigChangeEvent changeEvent) {
            //动态更新日志级别
            refreshLoggingLevels();
        }
    
        /**
         * 动态刷新日志级别
         */
        @PostConstruct
        private void refreshLoggingLevels() {
            Set<String> keyNames = config.getPropertyNames();
            for (String key : keyNames) {
                if (containsIgnoreCase(key, LOGGER_TAG)) {
                    String strLevel = config.getProperty(key, "info");
                    LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
                    loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
                    LOGGER.info("{}:{}", key, strLevel);
                }
            }
        }
    
        private boolean containsIgnoreCase(String str, String searchStr) {
            if (str == null || searchStr == null) {
                return false;
            }
            int len = searchStr.length();
            int max = str.length() - len;
            for (int i = 0; i <= max; i++) {
                if (str.regionMatches(true, i, searchStr, 0, len)) {
                    return true;
                }
            }
            return false;
        }
    
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        public String getProperty(String key) {
            return env.getProperty(key);
        }
    }
    

增加Apollo配置

  • 通过Apollo界面,在【applicaiton.proerties】配置中,增加需要控制的类配置

    logging.level.com.evan.controller.HelloController = info
    logging.level.com.evan.service.HelloService = ERROR
    logging.level.com.evan.mapper.HelloMapper = WARN
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值