logback设置不打印某个类的日志

本文介绍如何在Logback.xml文件中通过设置特定类名的日志级别为OFF来排除不需要打印的日志信息。这对于优化应用程序的日志输出非常有用。

logback.xml中只需要将类名配置进去,将level配置为OFF即可。

<!-- 排除不需要打印的日志 -->
<logger name="org.jeecg.modules.action.service.impl.ResUseOccupyServiceImpl" level="OFF" />  

在使用 **Logback** 作为日志框架时,如果你想在 **控制台打印调试日志(DEBUG)**,需要配置 `logback-spring.xml` 或 `logback.xml` 文件,并设置日志输出级别为 `DEBUG`。以下是完整的配置方法和说明。 --- ## ✅ 一、logback 配置文件位置 通常 `logback-spring.xml` 放在项目的 `resources` 目录下,Spring Boot 项目推荐使用 `logback-spring.xml`,支持 Spring 的 profile 配置。 ``` src/main/resources/logback-spring.xml ``` --- ## ✅ 二、配置控制台输出 DEBUG 级别日志 以下是一个完整的 `logback-spring.xml` 示例,配置控制台输出 **DEBUG 级别的日志**: ```xml <?xml version="1.0" encoding="UTF-8"?> <configuration debug="false" scan="true" scanPeriod="30 seconds"> <!-- 定义日志格式 --> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/> <!-- 控制台输出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <!-- 设置 root 日志级别为 DEBUG --> <root level="DEBUG"> <appender-ref ref="STDOUT"/> </root> <!-- 可选:单独设置某个包的日志级别 --> <logger name="com.example.demo" level="DEBUG"/> </configuration> ``` --- ## ✅ 三、Spring Boot 中通过 application.yml 设置日志级别(推荐) 如果你使用的是 **Spring Boot**,你也可以直接在 `application.yml` 中配置日志级别需要手动修改 `logback-spring.xml`: ```yaml logging: level: com.example.demo: debug ``` > 此配置表示 `com.example.demo` 包下的所有日志级别为 `DEBUG`。 --- ## ✅ 四、Java 代码中使用日志打印调试信息 确保你的 Java 中使用了 Logback日志接口(如 SLF4J): ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DemoService { private static final Logger logger = LoggerFactory.getLogger(DemoService.class); public void doSomething() { logger.debug("This is a debug message."); logger.info("This is an info message."); logger.warn("This is a warn message."); logger.error("This is an error message."); } } ``` --- ## ✅ 五、运行结果示例(控制台输出) ``` 2025-04-05 10:30:22.123 [main] DEBUG com.example.demo.DemoService - This is a debug message. 2025-04-05 10:30:22.124 [main] INFO com.example.demo.DemoService - This is an info message. 2025-04-05 10:30:22.125 [main] WARN com.example.demo.DemoService - This is a warn message. 2025-04-05 10:30:22.126 [main] ERROR com.example.demo.DemoService - This is an error message. ``` --- ## ✅ 六、常见问题排查 ### ❌ 问题:控制台打印 DEBUG 日志 #### 可能原因: 1. `logback.xml` 中 root level 设置为 INFO 2. 没有配置 `STDOUT` 控制台输出 3. 没有正确设置 logger 的 level 4. 使用了 `logback.xml` 而是 `logback-spring.xml`(Spring Boot 中) --- ##
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cgv3

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

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

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

打赏作者

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

抵扣说明:

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

余额充值