SpringBoot运行中动态修改logback日志级别
思路:写一个api接口,通过api接口调用的方式动态修改logback的log日志打印级别
这里提供2个接口,分别是修改logback全局日志级别 ,和单独修改某个package包的日志级别
package cn.demo.rest;import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;@Slf4j
@RestController
public class CloudApi {//--------------------------------------------------------------------/*
http://localhost:8627/iotcenter/alterLogLevel/info*/@GetMapping("/alterSysLogLevel/{level}")public String alterSysLogLevel(@PathVariable String level){LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();context.getLogger("ROOT").setLevel(Level.valueOf(level));return "ok";}/*
http://localhost:8627/iotcenter/alterPkgLogLevel?level=info&pkgName=com.xx.xxx*/@GetMapping("/alterPkgLogLevel")public String alterPkgLogLevel(@RequestParam String level, @RequestParam String pkgName){LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();context.getLogger(pkgName).setLevel(Level.valueOf(level));return "ok";}}