当前位置: 首页 > ai >正文

Logback-spring.xml 配置屏蔽特定路径的日志

在 Spring Boot 项目中,使用 logback-spring.xml 配置屏蔽特定路径的日志有两种常用方式:

方案一:基础配置(直接关闭目标路径日志)

<?xml version="1.0" encoding="UTF-8"?>
<configuration><!-- 屏蔽 com.example.sensitive 包及其子包的所有日志 --><logger name="com.example.sensitive" level="OFF" /><!-- 若需精确屏蔽特定类 --><logger name="com.example.service.SensitiveService" level="OFF" /><!-- Spring Boot 默认控制台输出 --><include resource="org/springframework/boot/logging/logback/defaults.xml" /><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern></encoder></appender><root level="INFO"><appender-ref ref="CONSOLE" /></root>
</configuration>

方案二:结合 Spring Profile 按环境屏蔽

<?xml version="1.0" encoding="UTF-8"?>
<configuration><springProfile name="prod"><!-- 生产环境屏蔽指定包日志 --><logger name="com.example.debug" level="OFF" /></springProfile><springProfile name="dev,test"><!-- 开发/测试环境保留全部日志 --><logger name="com.example.debug" level="DEBUG" /></springProfile><!-- 公共配置 --><include resource="org/springframework/boot/logging/logback/defaults.xml" /><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern></encoder></appender><root level="INFO"><appender-ref ref="CONSOLE" /></root>
</configuration>

关键配置说明:

  1. 精准路径屏蔽

    <logger name="完整的包或类路径" level="OFF" />
    • name 属性:支持包路径(如 com.example.util)或全限定类名(如 com.example.util.CryptoUtils
    • 包路径会屏蔽该包及其所有子包下的日志
  2. 避免日志传递​(可选)
    添加 additivity="false" 防止日志事件向上传递:

    <logger name="com.example.noisy" level="OFF" additivity="false" />
  3. 使用 Spring Profile
    <springProfile> 标签支持基于环境变量动态控制:

    <!-- 多环境控制示例 -->
    <springProfile name="!prod">  <!-- 非生产环境生效 --><logger name="com.example.temp" level="DEBUG" />
    </springProfile>

验证生效:

  1. 检查路径匹配​:

    • 包路径:com.example.sensitive 会屏蔽:
      • com.example.sensitive.Service
      • com.example.sensitive.util.Helper
      • 等所有子包中的类
  2. 测试日志输出​:

    // 被屏蔽的类
    package com.example.sensitive;public class SecureService {private static final Logger log = LoggerFactory.getLogger(SecureService.class);public void process() {log.debug("这条日志应该被隐藏"); // 不会输出log.error("这条日志也会被隐藏"); // OFF 级别会屏蔽所有级别}
    }

常见问题解决:

  1. 屏蔽不生效​:

    • 检查路径是否正确(区分大小写)
    • 确保没有其他配置覆盖(如根 logger 设置)
    • 确认配置位置:src/main/resources/logback-spring.xml
  2. 部分屏蔽​:

    • 若需保留错误日志:
      <logger name="com.example.large" level="ERROR" /> <!-- 只显示 ERROR 及以上 -->
  3. 环境变量控制​:

    • 启动时指定 Profile:
      java -jar app.jar --spring.profiles.active=prod

提示:Spring Boot 会自动加载 logback-spring.xml 并支持热更新(默认扫描间隔 30 秒),无需重启应用即可生效。

http://www.xdnf.cn/news/13957.html

相关文章:

  • Vue3+Element Plus动态表格列宽设置
  • 【写实交互数字人】实时数字人助力政务变革:技术、产品与应用价值的全景剖析
  • 【插件推荐】WebRTC Protect — 防止 IP 泄漏
  • 苹果越来越像安卓,华为越来越像苹果
  • 电路图识图基础知识-电动机软启动器技术解析与应用(二十五)
  • 【Zephyr 系列 22】从单机开发到平台化:构建你自己的 Zephyr 物联网开发平台
  • 【结合JSR380自定义校验】
  • Altera系列FPGA基于ADV7180解码PAL视频,纯verilog去隔行,提供2套Quartus工程源码和技术支持
  • 智慧物流园区——解读华为智慧物流园区解决方案【附全文阅读】
  • 上海市计算机学会竞赛平台2022年4月月赛丙组圆环独立集(一)
  • 基于 Spring Cloud Gateway + Sentinel 实现高并发限流保护机制
  • PHP基础-控制结构
  • 全链路实时感知:网络专线端到端监控运维
  • SwiftUI隐藏返回按钮保留右滑手势方案
  • MyBatis原理
  • 关于阿里云-云消息队列MQTT的连接和使用,以及SpringBoot的集成使用
  • P8784 [蓝桥杯 2022 省 B] 积木画
  • 基于 STM32 七段数码管显示模块详解
  • 如何设置爬虫的访问频率?
  • 基于51单片机的直流电机运动控制proteus仿真
  • vue二级路由的写法,以及动态路由的匹配和获取动态参数的值
  • FreeSWITCH mod_curl 和 mod_xml_rpc 测试
  • JVM 内存、JMM内存与集群机器节点内存的联系
  • 【redis——缓存穿透】
  • 基于PSO粒子群优化的VMD-LSTM时间序列预测算法matlab仿真
  • git 下载安装并连接gitee
  • 一键给你的网页增加 ios26 液态玻璃效果
  • Android 手机如何实现本地视频音频提取?实战教程来了
  • 提示词Prompts(2)
  • 提示词Prompts(1)