首页 > 代码库 > java 中用程序修改log4j的输出等级

java 中用程序修改log4j的输出等级

    public static void SetLogLevel(String strAppender, String level){
        if(level == null || strAppender == null)
            return;
        
        Appender appender = Logger.getRootLogger().getAppender(strAppender);
        Level levelSet = Level.toLevel(level);
        if (levelSet != null && appender != null){
            Filter filterIt = appender.getFilter();
            
            while(filterIt != null){
                if(filterIt instanceof LevelRangeFilter){
                    LevelRangeFilter f = (LevelRangeFilter)filterIt;
                    f.setLevelMin(levelSet);
                    break;
                }
                filterIt = filterIt.getNext();
            }
            
            Logger.getRootLogger().setLevel(levelSet);
        }
    }

 

java 中用程序修改log4j的输出等级