Spring Boot 日志配置通用規律。想看特定日志,怎么打開日志開關
文章目錄
- 一、一句話總結
- 二、AI問答版
- 提問詞
- AI的響應
- 📊 Spring Boot 日志配置通用規律
- 1. 基本語法結構
- 2. 日志級別(從詳細到簡潔)
- 🎯 常用日志配置分類
- 1. Spring Cloud Gateway 相關
- 2. Web 和 HTTP 相關
- 3. 數據庫和SQL相關(您問的重點)
- 4. Spring 核心框架
- 5. Eureka 服務發現
- 🔍 按場景推薦的日志配置
- 場景1:網關路由調試
- 場景2:SQL和數據庫調試
- 場景3:完整的微服務調試
- 場景4:性能監控
- 🛠? 高級配置技巧
- 1. 按環境配置不同的日志級別
- 2. 輸出到文件和控制臺
- 3. 使用Logback高級配置
- 📝 快速記憶口訣
- 🔎 如何找到正確的包名?
一、一句話總結
一句話描述就是找到要觀察的類(從異常堆棧信息中獲取類名),寫上全類名和等級。yml內打開日志的配置形如:
logging:level:
# org.springframework.cloud.gateway: TRACE
# org.springframework.http.server.reactive: DEBUG
# org.springframework.cloud.netflix.eureka: DEBUG
# com.netflix.eureka: DEBUG
# com.netflix.discovery: DEBUG
# com.xxx.mapper: DEBUG
二、AI問答版
提問詞
總結一下此類日志如何打開,有時我想查看SQL打印日志,但不知道怎么開,但是這類應該有規律:logging:level:com.netflix.discovery: DEBUG
AI的響應
您說得對!Spring Boot的日志配置確實有規律可循。我來為您總結一下各類日志的開啟方法。
📊 Spring Boot 日志配置通用規律
1. 基本語法結構
logging:level:# 包名或類全限定名: 日志級別com.example.service: DEBUGorg.springframework.web: INFO
2. 日志級別(從詳細到簡潔)
TRACE
>DEBUG
>INFO
>WARN
>ERROR
>OFF
🎯 常用日志配置分類
1. Spring Cloud Gateway 相關
logging:level:org.springframework.cloud.gateway: TRACE # 網關核心日志org.springframework.cloud.gateway.handler: DEBUGorg.springframework.cloud.gateway.route: DEBUGorg.springframework.cloud.gateway.filter: DEBUGreactor.netty: DEBUG # 網絡層日志
2. Web 和 HTTP 相關
logging:level:org.springframework.web: DEBUG # Spring Web MVCorg.springframework.web.reactive: DEBUG # WebFlux (Gateway用)org.springframework.http: DEBUG # HTTP請求響應org.springframework.http.server.reactive: DEBUG
3. 數據庫和SQL相關(您問的重點)
logging:level:# Hibernate SQL日志org.hibernate.SQL: DEBUG # 顯示執行的SQL語句org.hibernate.type.descriptor.sql: TRACE # 顯示SQL參數值org.hibernate.stat: DEBUG # Hibernate統計信息# JPA日志org.springframework.data.jpa: DEBUGorg.springframework.orm.jpa: DEBUG# MyBatis日志com.example.mapper: DEBUG # MyBatis Mapper接口org.mybatis: DEBUG# 數據庫連接池com.zaxxer.hikari: DEBUG # HikariCP連接池org.apache.tomcat.jdbc.pool: DEBUG # Tomcat連接池
4. Spring 核心框架
logging:level:org.springframework.context: DEBUG # Spring上下文org.springframework.beans: DEBUG # Bean管理org.springframework.transaction: DEBUG # 事務管理
5. Eureka 服務發現
logging:level:org.springframework.cloud.netflix.eureka: DEBUGcom.netflix.eureka: DEBUGcom.netflix.discovery: DEBUG
🔍 按場景推薦的日志配置
場景1:網關路由調試
logging:level:org.springframework.cloud.gateway: TRACEorg.springframework.http.server.reactive: DEBUGreactor.netty: DEBUGorg.springframework.cloud.netflix.eureka: DEBUG
場景2:SQL和數據庫調試
logging:level:org.hibernate.SQL: DEBUGorg.hibernate.type.descriptor.sql: TRACEcom.zaxxer.hikari: DEBUGorg.springframework.transaction: DEBUG
場景3:完整的微服務調試
logging:level:# 網關org.springframework.cloud.gateway: DEBUG# 數據庫org.hibernate.SQL: DEBUGorg.hibernate.type: TRACE# 服務發現org.springframework.cloud.netflix.eureka: DEBUG# Web請求org.springframework.web: DEBUGorg.springframework.web.reactive: DEBUG
場景4:性能監控
logging:level:org.hibernate.stat: DEBUG # Hibernate性能統計com.zaxxer.hikari: DEBUG # 連接池監控org.springframework.transaction: DEBUG # 事務監控
🛠? 高級配置技巧
1. 按環境配置不同的日志級別
# application-dev.yml (開發環境)
logging:level:org.hibernate.SQL: DEBUGorg.springframework.cloud.gateway: TRACE# application-prod.yml (生產環境)
logging:level:org.hibernate.SQL: WARNorg.springframework.cloud.gateway: INFO
2. 輸出到文件和控制臺
logging:file:name: logs/gateway.log # 輸出到文件level:org.springframework.cloud.gateway: DEBUGpattern:console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
3. 使用Logback高級配置
創建 logback-spring.xml
進行更精細的控制。
📝 快速記憶口訣
- 找包名:
org.springframework.xxx
- 加級別:
DEBUG
或TRACE
- 看效果:觀察控制臺輸出
- 調精度:根據需要調整級別
🔎 如何找到正確的包名?
- 查看異常堆棧:從異常信息中獲取類名
- 使用IDE搜索:在IDE中搜索相關關鍵詞
- 官方文檔:查閱框架文檔中的日志配置章節
- 通用規律:
- Spring:
org.springframework.xxx
- Hibernate:
org.hibernate.xxx
- 數據庫:
com.zaxxer.hikari
,org.apache.tomcat.jdbc
- 項目代碼:
com.yourcompany.xxx
- Spring:
掌握了這個規律,您就可以輕松配置任何需要的日志級別了!