Spring Boot中slf4j日志依賴關(guān)系示例詳解
前言
SpringBoot底層使用的是slf4j+logback來進(jìn)行日志記錄
把其他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)換包進(jì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)日志級(jí)別為INFO級(jí)別
日志優(yōu)先級(jí)從小到大順序?yàn)椋?/p>
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日志");
}
}
啟動(dòng)運(yùn)行,控制臺(tái)打印只打印了info及以上級(jí)別
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ǔ)配置
# 指定日志輸入級(jí)別
logging.level.com.example.demo=trace
# 指定日志輸出位置和日志文件名
logging.file=./log/log.txt
# 指定日志輸出路徑,若file和path同時(shí)配置,則file生效
# 此配置默認(rèn)生成文件為spring.log
#logging.path=./log
# 控制臺(tái)日志輸出格式
# -5表示從左顯示5個(gè)字符寬度
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é):
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java使用DOM4j實(shí)現(xiàn)讀寫XML文件的屬性和元素
這篇文章主要為大家詳細(xì)介紹了Java使用DOM4j實(shí)現(xiàn)讀寫XML文件的屬性和元素,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
基于Java實(shí)現(xiàn)的一層簡(jiǎn)單人工神經(jīng)網(wǎng)絡(luò)算法示例
這篇文章主要介紹了基于Java實(shí)現(xiàn)的一層簡(jiǎn)單人工神經(jīng)網(wǎng)絡(luò)算法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)人工神經(jīng)網(wǎng)絡(luò)的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
Spring?session?redis?修改默認(rèn)的序列化方法(案例)
這篇文章主要介紹了Spring?session?redis?修改默認(rèn)的序列化方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
openGauss數(shù)據(jù)庫JDBC環(huán)境連接配置的詳細(xì)過程(Eclipse)
這篇文章主要介紹了openGauss數(shù)據(jù)庫JDBC環(huán)境連接配置(Eclipse),演示基于JDBC開發(fā)的主要步驟,會(huì)涉及創(chuàng)建數(shù)據(jù)庫、創(chuàng)建表、插入數(shù)據(jù)等,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
mybatis-plus用insertBatchSomeColumn方法批量新增指定字段
mybatisPlus底層的新增方法是一條一條的新增的,下面這篇文章主要給大家介紹了關(guān)于mybatis-plus用insertBatchSomeColumn方法批量新增指定字段的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

