动态修改日志级别
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);
}
}