1. 在application.yml 通过配置配置,按住Ctrl + 左键可以看源码
logging:
level:
root: info
com.dj.test.controller: debug
com.dj.test.service: debug
2. 自己写接口直接动态修改日志级别,读取application.yml配置文件算是间接修改文章来源:https://www.toymoban.com/news/detail-803712.html
package com.dj.springtest.controller;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
/**
* Date: 2024/1/13
* Time: 22:02
* Description: No Description
*/
@RestController
public class LogController {
private static final List<String> LEVELS = Arrays.asList("ALL", "all", "TRACE", "trace", "DEBUG", "debug", "INFO", "info", "WARN", "warn", "ERROR", "error", "OFF", "off");
/**
* 设置日志界级别
*
* @param overall 全局设置
* @param part 部分设置
* @param packagePath 涵盖包,如果有多个path,请使用英文逗号分隔
*/
@GetMapping("/log/level")
public String setLogLevel(String overall, String part, String packagePath) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
if (StringUtils.isNotBlank(overall) && LEVELS.contains(overall)) {
Logger rootLogger = loggerContext.getLogger("root");
rootLogger.setLevel(Level.toLevel(overall));
}
//根据包路径进行设置
if (StringUtils.isNotBlank(part) && LEVELS.contains(part) && StringUtils.isNotBlank(packagePath)) {
String[] paths = packagePath.split(",");
for (String path : paths) {
Logger pathLogger = loggerContext.getLogger(path);
pathLogger.setLevel(Level.toLevel(part));
}
}
return "Succeeded in changing the log level";
}
}
文章来源地址https://www.toymoban.com/news/detail-803712.html
到了这里,关于SpringBoot Logback修改日志级别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!