欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

动态修改日志级别  

程序员文章站 2023-12-26 20:40:21
...


import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 

  private void updateLogLevel(String levelString) {
        if (StringUtils.isNotBlank(levelString) && !"null".equalsIgnoreCase(levelString)) {
            Enumeration logs = LogManager.getCurrentLoggers();
            logger.error("begain updateLogLevel,level:{}", levelString);
            while (logs.hasMoreElements()) {
                org.apache.log4j.Logger logger = (org.apache.log4j.Logger) logs.nextElement();
                //zookeeper中的不管,有些类的debug日志太恐怖
                if(logger.getName().indexOf("org.apache.zookeeper")>=0){
                    continue;
                }
                logger.error("before package name "+logger.getName()+",and level:"+logger.getEffectiveLevel());

                Level level = Level.toLevel(levelString);
                logger.setLevel(level);
                logger.error("after package name "+logger.getName()+",and level:"+logger.getEffectiveLevel());
            }
            logger.error("after updateLogLevel,level:{}", levelString);
        }
    }

上一篇:

下一篇: