本文是這篇教程的學習筆記:
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.
將日志寫入本地日志文件的做法:

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

