spring boot 动态修改日志级别

spring boot 版本 2.3.3

 

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggerConfiguration;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private LoggingSystem loggingSystem;

    @GetMapping("")
    public Object getActiveProfiles() {
        log.info("test");
        List<LoggerConfiguration> loggerConfigurations = loggingSystem.getLoggerConfigurations();
        return loggerConfigurations;
    }

    @GetMapping("update")
    public Object update(
            @RequestParam String name,
            @RequestParam LogLevel level
    ) {
        loggingSystem.setLogLevel(name,level);
        return "ok";
    }
}

 

posted @ 2020-09-10 15:03  yang91  阅读(314)  评论(0)    收藏  举报