天天看點

SpringBoot裡slf4j日志功能的預設實作

本文是這篇教程的學習筆記:

https://www.springboottutorial.com/logging-with-spring-boot-logback-slf4j-and-log4j

我有一個最簡單的SpringBoot應用,其pom.xml裡的依賴定義如下:

上圖看到很多Starter,比如Spring-boot-starter-data-redis, Spring-boot-starter-web等等。這些Starter都包含了一個spring-boot-starter, 而這個starter又包含了一個實作logging的starter,叫做spring-boot-starter-logging.

也就是說,如果我們使用SpringBoot,無需再導入額外的配置,就能使用日志功能了。

SpringBoot slf4j的預設實作是logback,預設輸出是到控制台console:

As you can see the default logging framework is Logback with SLF4j as implementation.

By default, all logging goes to console.

在Application.properties檔案裡使用logging.level.root配置項目全局的日志級别:

當然要針對個别包進行日志級别的分别配置也是可以的:

logging.level.some.package.path=DEBUG

logging.level.some.other.package.path=ERROR

Custom configuration using logback.xml

Spring Boot will pick up all custom configuration using logback.xml as long as it is in the application class path.

将日志寫入本地日志檔案的做法:

本文來自雲栖社群合作夥伴“汪子熙”,了解相關資訊可以關注微信公衆号"汪子熙"。