動態的修改線上環境的日誌級別,在分佈式環境,一次修改一臺,改變的途徑有:
(1)通過http訪問,處理請求,改變日誌級別。可以使用輕量級的httpServer(推薦 NanoHTTPD)監聽指定端口,http請求ip,訪問到具體的機器上修改日誌級別。
(2)通過請求Zookeeper,修改節點上的日誌級別信息,修改指定ip的日誌級別。
修改日誌級別的代碼:
public static void main(String[] args) {
// 獲取日誌類型
String type = StaticLoggerBinder.getSingleton().getLoggerFactoryClassStr();
System.out.println(type);
// 獲取日誌內部狀態信息
LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
// 獲取所有的logger
List<ch.qos.logback.classic.Logger> list = lc.getLoggerList();
System.out.println("before---"+JsonUtil.toJSONString(list));
// 更改日誌級別
list.get(0).setLevel(Level.INFO);
System.out.println("after---"+JsonUtil.toJSONString(lc.getLoggerList()));
// 使用StatusPrinter打印日誌內部狀態
StatusPrinter.print(lc);
// 看下默認的日誌格式
logger.info("DesUtils -> main, args= test.test");
}
NanoHTTPD:
GitBub: https://github.com/NanoHttpd/nanohttpd
CSDN: https://blog.csdn.net/qincidong/article/details/82656569