Spring Boot中slf4j日志依賴關(guān)系示例詳解
前言
SpringBoot底層使用的是slf4j+logback來進行日志記錄
把其他common-logging、log4j、java.util.logging轉(zhuǎn)換為slf4j
下面這篇文章主要給大家介紹了關(guān)于Spring Boot slf4j日志依賴關(guān)系的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧
底層依賴關(guān)系

關(guān)系如何轉(zhuǎn)化

底層通過偷梁換柱的方法,用jcl、jul、log4j中間轉(zhuǎn)換包進行轉(zhuǎn)化

如果要引入其他框架,必須將其中默認(rèn)日志依賴剔除
SpringBoot從maven依賴中剔除springframework:spring-core中的common-logging
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.20.RELEASE</version> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> </exclusions> </dependency>
SpringBoot默認(rèn)日志級別為INFO級別
日志優(yōu)先級從小到大順序為:
trace<debug<info<warn<error
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
Logger log = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
log.trace("trace日志");
log.debug("debug日志");
log.info("info日志");
log.warn("warn日志");
log.error("error日志");
}
}
啟動運行,控制臺打印只打印了info及以上級別
2018-11-09 00:13:36.899 INFO 8156 --- [main] com.example.demo.DemoApplicationTests : info日志
2018-11-09 00:13:36.900 WARN 8156 --- [main] com.example.demo.DemoApplicationTests : warn日志
2018-11-09 00:13:36.900 ERROR 8156 --- [main] com.example.demo.DemoApplicationTests : error日志
日志基礎(chǔ)配置
# 指定日志輸入級別
logging.level.com.example.demo=trace
# 指定日志輸出位置和日志文件名
logging.file=./log/log.txt
# 指定日志輸出路徑,若file和path同時配置,則file生效
# 此配置默認(rèn)生成文件為spring.log
#logging.path=./log
# 控制臺日志輸出格式
# -5表示從左顯示5個字符寬度
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %boldYellow(%thread) | %boldGreen(%logger) | %msg%n
# 文件中輸出的格式
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} = [%thread] = %-5level = %logger{50} - %msg%n
總結(jié):
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
MybatisPlus中@TableField注解的使用詳解
這篇文章主要介紹了MybatisPlus中@TableField注解的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot如何對LocalDateTime進行格式化并解析
這篇文章主要介紹了SpringBoot如何對LocalDateTime進行格式化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Mybatis-Plus默認(rèn)主鍵策略導(dǎo)致自動生成19位長度主鍵id的坑
這篇文章主要介紹了Mybatis-Plus默認(rèn)主鍵策略導(dǎo)致自動生成19位長度主鍵id的坑,本文一步步給大家分享解決方法,給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12

