springboot+logback动态修改日志级别

在Spring Boot 项目 中,使用了 Logback(logback.xml)​ 作为日志框架。

@Slf4j
public class MqttTransportHandler {
    public void someMethod() {
        log.debug("This is a debug message");
    }
}

在系统运行期间,MqttTransportHandler 中 someMethod()日志是不会输出的 ,因为我们logback生产日志只打印info级别。
在 Spring Boot + Logback 的默认配置中:

  • 通常 默认的日志级别是 INFO​(或者由 logback.xml / application.yml 中定义)
  • DEBUG 级别的日志,在日志级别为 INFO 或更高时,是不会输出的!
  • 所以,即使你代码里写了 log.debug(“xxx”),​如果当前 Logger 的级别是 INFO,那就不会打印!​
    目的:指定类名 包名.MqttTransportHandler 的日志级别为debug 模式。

方案 一 接口控制方式

接口控制方式 我任务最简单,最灵活。

import org.springframework.boot.logging.LoggingSystem;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/logging")
public class LogLevelController {

    @Autowired
    private LoggingSystem loggingSystem;

    @PostMapping("/level")
    public ResponseResult setLogLevel(
            @RequestParam String loggerName,
            @RequestParam String level) {
        
        loggingSystem.setLogLevel(loggerName, LogLevel.valueOf(level));
        log.info("==> loggerName:{}, level:{}", loggerName, level);
        return ResponseResult.success(true);
    }
}

logback.xml 要去掉 filter 里面的info 如下:

<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter> 要去掉 filter 里面的info  由下面 root  控制默认输出级别 INFO-->
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        <charset>UTF-8</charset>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="STDOUT" />
 </root>   
  

然后编写测试类 假设 com.laich.server.controller.boke.ApiDeviceController 里面有debug(“日志”)
接口 loggerName = com.laich.server.controller.boke.ApiDeviceController
level = DEBUG 测试成功的话,debug日志会输出来。

方案 二:​使用 Spring Boot Actuator 的 loggers 端点

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在 application.yml 或 application.properties 中配置)

management:
  endpoints:
    web:
      exposure:
        include: health,info,loggers  # 加上 loggers
  endpoint:
    loggers:
      enabled: true

或者 properties 格式:

management.endpoints.web.exposure.include=health,info,loggers
management.endpoint.loggers.enabled=true

访问如下 URL(假设你的类所在包是 com.example.mqtt):

GET http://localhost:8080/actuator/loggers/com.example.mqtt.MqttTransportHandler

你会看到类似这样的返回:

{ 
 "configuredLevel": null,
  "effectiveLevel": "INFO"
}
  • configuredLevel: 是你显式为该 Logger 设置的级别(比如 DEBUG),如果没有就是 null
  • effectiveLevel: 是当前实际生效的级别(比如 INFO,来自 root 或父 Logger)

动态修改某个 Logger 的日志级别为 DEBUG
发送一个 POST 请求:

http://localhost:8080/actuator/loggers/com.example.mqtt.MqttTransportHandler
Content-Type: application/json
{
  "configuredLevel": "DEBUG"
}

🎯 把 com.example.mqtt.MqttTransportHandler 替换为你实际的 Logger 名称,通常是 类所在的包名.类名,或者它的父包(如 com.example.mqtt)
5. 再次调用你的方法,log.debug(“xxx”) 就会输出了!​
✅ 优点:​

  • 官方支持,安全可靠
  • 无需重启服务,实时生效
  • 可针对某个类 / 包单独设置日志级别
  • 支持查看当前生效级别
    ✅ 缺点:​
  • 需要启用 actuator,并做好安全控制(生产环境别暴露所有端点!)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芳心村

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值