@RestController
public class LogbackController {
/**
* logback動態修改包名的日誌級別
* @param level 日誌級別
* @param packageName 包名
* @return
* @throws Exception
*/
@RequestMapping(value = "/level")
public void updateLogbackLevel(@RequestParam(value="level") String level,
@RequestParam(value="packageName",defaultValue = "-1") String packageName, HttpServletResponse response) throws Exception {
ch.qos.logback.classic.LoggerContext loggerContext =(ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
if(packageName.equals("-1")) {
// 預設值-1,更改全域性日誌級別;否則按傳遞的包名或類名修改日誌級別。
loggerContext.getLogger("root").setLevel(ch.qos.logback.classic.Level.toLevel(level));
} else {
loggerContext.getLogger(packageName).setLevel(ch.qos.logback.classic.Level.valueOf(level));
}
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.print("修改成功!");
}
}