Java Log4j2 動態調整log級別小工具
阿新 • • 發佈:2018-12-19
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.LoggerConfig; import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author: gaobangpeng * @Date: 2018/11/1 14:23 * @Version 1.0 */ public class DynamicGetAndSetLog { private static final Logger LOGGER = LogManager.getLogger(DynamicGetAndSetLog.class); /** * 獲取某個類或包的日誌級別 * * @param loggerName 類或包名 * @return String */ public static String getLoggerLevel(String loggerName) { if (loggerName == null || "".equals(loggerName)) { return "目標類或包不能為空"; } Logger logger = LogManager.getLogger(loggerName); if (logger != null) { return logger.getLevel().toString(); } return "當前log等級獲取失敗"; } /** * 修改某個類或包的日誌級別 * * @param loggerName 類或包名 * @param level log級別 * @return String */ public static String setLogLevel(String loggerName, String level) { try { Level newLevel = Level.valueOf(level); LoggerContext logContext = (LoggerContext) LogManager.getContext(false); Configuration configuration = logContext.getConfiguration(); LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName); // getLoggerConfig("a.b.c") could return logger for "a.b" if there is no logger for "a.b.c" if (loggerConfig.getName().equalsIgnoreCase(loggerName)) { loggerConfig.setLevel(newLevel); } else { // create a new config. loggerConfig = new LoggerConfig(loggerName, newLevel, false); configuration.addLogger(loggerName, loggerConfig); LoggerConfig parentConfig = loggerConfig.getParent(); do { for (Map.Entry<String, Appender> entry : parentConfig.getAppenders().entrySet()) { loggerConfig.addAppender(entry.getValue(), null, null); } parentConfig = parentConfig.getParent(); } while (null != parentConfig && parentConfig.isAdditive()); } logContext.updateLoggers(); } catch (Exception e) { LOGGER.info(e); return "設定log級別失敗"; } return "設定log級別為" + level; } /** * 通過包名獲取包下所有class類,以及每個類對應的Log級別 * * @param packageName 包名 * @return Map <類名,log級別> */ public static Map<String, String> getAllClassesAndLogLevelInCurrentPackage(String packageName) { List<String> res = null; try { res = PackageUtil.getClassName(packageName); } catch (IOException e) { LOGGER.error("獲取包下所有類失敗" + e); } Map<String, String> result = new HashMap<>(); String regex = "\\"; for (String classFile : res) { try { classFile = classFile.replace(regex, "."); classFile = classFile.substring(classFile.indexOf(packageName)); } catch (Exception e) { LOGGER.error("包名替換,出現異常," + e); } try { Class buildingController = Class.forName(classFile); Field[] fields = buildingController.getDeclaredFields(); for (int s = 0; s < fields.length; s++) { if (fields[s].getName().equals("LOGGER")) { String logLevel = getLoggerLevel(classFile); result.put(classFile, logLevel); break; } } } catch (ClassNotFoundException e) { LOGGER.error("沒有發現該類" + classFile); } } return result; } }