springboot中动态修改logback日志级别
在spring boot中使用logback日志时,项目运行中,想要修改日志级别。
代码如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Author: Zhy
* @Description:
* @Date: Created in 8:28 PM 2019/5/15
* @Modified By:
*/
public class MainTest {
private static Loggerlogger = LoggerFactory.getLogger(MainTest.class);
public static void main(String[] args) {
ch.qos.logback.classic.LoggerContext loggerContext =
(ch.qos.logback.classic.LoggerContext) LoggerFactory.getILoggerFactory();
System.out.println(loggerContext.getLogger("root").getLevel());
logger.info("hello info:111");
logger.error("hello error:111");
//直接调用"/level" 接口即可修改日志级别。
loggerContext.getLogger("root").setLevel(ch.qos.logback.classic.Level.toLevel(40000));
loggerContext.getLogger("root").getLevel();
logger.info("hello info:222");
logger.error("hello error:222");
}
}
控制台输出:
DEBUG
15:28:41.943 [main] INFO MainTest - hello info:111
15:28:41.947 [main] ERROR MainTest - hello error:111
15:28:41.947 [main] ERROR MainTest - hello error:222