代码如下
package com.wbcloudhealth.biz.uc.user.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* logback日志级别调整接口
*
* @author
* @Description logback日志级别调整接口
* @createTime 2021/6/15 16:19
*/
@RestController
@RequestMapping("/logback")
public class LogbackController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
/**
*@Description 动态的修改日志的输级别
*@Param level-日志级别,值的选择范围 DEBUG、INFO、WARN、ERROR
*@Author
*@CreateTime 2021/6/15 16:23
*@Return
*/
@RequestMapping(value = "/setLevel")
public String setLevel( @RequestParam(value="level") String level,
@RequestParam(value="packageName",defaultValue = "-1") String packageName) throws Exception {
logger.info("=========LogbackController.setLevel=======接收到请求==========");
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));
}
return "success";
}
}
请求参数如下