1. 程式人生 > >springboot程式logback日誌基本配置,多個包不同日誌級別輸入到檔案中

springboot程式logback日誌基本配置,多個包不同日誌級別輸入到檔案中

日誌是程式中必不可少的內容。依據日誌我們可以有效診斷程式bug,統計使用者訪問和各主要功能的使用頻率時間段等資訊。因此我們會需要不同package使用不同的日誌級別,以及不同業務的日誌輸出到不同的檔案。下面本文簡要概述如何使用logback將不同包的不同級別日誌輸出到info1.txt中。

本文的程式碼在這裡,歡迎加星,fork。
1, springboot配置日誌logback日誌的方式
官方文件在https://docs.spring.io/spring-boot/docs/1.5.12.RELEASE/reference/html/boot-features-logging.html#boot-features-logging-format

我們一般只需要在application.properties中設定類似如下的配置即可。

logging.config=classpath:logback-spring2.xml

2,程式的基本結構
程式是標準的springboot程式,基於springboot 1.5.12, 使用swagger訪問rest。包含service包和controller包。

在這裡插入圖片描述

3,logback.xml配置
簡要介紹,logback配置檔案中有3個appender, 分別為, a,控制檯(級別為debug)輸出到console中,b,FILE(沒有定義級別)輸出到logs/info1.log中,自動歸檔每天的日誌,修改為2018-11-10.txt這樣儲存起來。只保留最近7天的日誌。c,ERROR_FILE與FILE類似,只記錄error級別的日誌。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <!-- 控制檯輸出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter"
>
<level>DEBUG</level> <!-- 日誌過濾級別 --> </filter> <encoder> <pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化輸出 --> <charset>utf8</charset> <!-- 輸出編碼 --> </encoder> </appender> <!-- 檔案輸出 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化輸出 --> <charset>utf8</charset> <!-- 輸出編碼 --> </encoder> <file>./logs/info1.log</file><!-- 檔案存放路徑 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/logs/info/%d{yyyy-MM-dd}.log</fileNamePattern><!-- 每天歸檔 --> <maxHistory>7</maxHistory><!-- 日誌存放週期(天) --> </rollingPolicy> </appender> <!-- 錯誤日誌檔案輸出 --> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> <!-- 日誌過濾級別 --> </filter> <encoder> <pattern>%date %-5level [${HOSTNAME} %thread] %caller{1}%msg%n</pattern><!-- 格式化輸出 --> <charset>utf8</charset> <!-- 輸出編碼 --> </encoder> <file>./logs/error1.log</file><!-- 檔案存放路徑 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/logs/error/%d{yyyy-MM-dd}.log</fileNamePattern><!-- 每天歸檔 --> <maxHistory>7</maxHistory><!-- 日誌存放週期(天) --> </rollingPolicy> </appender> <!-- 基礎日誌輸出級別 --> <root level="DEBUG"> <appender-ref ref="CONSOLE"/> <appender-ref ref="ERROR_FILE"/> </root> <logger name="com.yq.controller" level="trace" additivity="false"> <appender-ref ref="FILE" /> </logger> <logger name="com.yq.service" level="info" additivity="false"> <appender-ref ref="FILE" /> </logger> <logger name="org.springframework.beans" level="debug" additivity="false"> <appender-ref ref="FILE" /> </logger> </configuration>

xml檔案解釋

com.yq.controller包trace級別及其以上的日誌,com.yq.service包info級別及其以上的日誌,org.springframework.beans包debug級別及其以上的日誌會記錄在FILE中,也就是記錄到info1.txt中

啟動程式後訪問http://127.0.0.1:9090/swagger-ui.html#/user-controller/getUserUsingGET, 點選getUser方法。
然後檢視控制檯日誌,程式所在目錄下的logs目錄中的info1.txt和error1.txt。

結果,按照如上配置,
info1.txt記錄了很多springboot的日誌包括debug級別的日誌,
com.yq.controller包下面的所有trace級別以上資訊
com.yq.service包下面的所有info級別以上的資訊
org.springframework.beans包下面的所有debug級別以上的資訊
error1.txt只記錄com.yq.LogbackApplication的錯誤日誌,沒有com.yq.controller和com.yq.controller的錯誤
console中controller包和servcie包都沒有記錄, 只有springboot的com.yq.LogbackApplication的日誌

info1.txt內容為
Returning cached instance of singleton bean ‘mvcViewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘defaultViewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘viewResolver’
2018-11-10 15:23:26,769 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘thymeleafViewResolver’
2018-11-10 15:23:26,770 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
Creating instance of bean ‘org.springframework.web.servlet.support.SessionFlashMapManager’
2018-11-10 15:23:26,776 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
Finished creating instance of bean ‘org.springframework.web.servlet.support.SessionFlashMapManager’
2018-11-10 15:23:26,807 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
Returning cached instance of singleton bean ‘userController’
2018-11-10 15:23:26,829 ERROR [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:40)
error find user=2 by id=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 WARN [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:41)
warn find user=2 by id=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 INFO [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.service.impl.UserServiceImpl.getById(UserServiceImpl.java:42)
info find user=2 by id=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018)
2018-11-10 15:23:26,830 ERROR [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:37)
error rest get user=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 WARN [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:38)
warn rest get user=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 INFO [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:39)
info rest get user=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:40)
debug rest get user=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018) by id=2
2018-11-10 15:23:26,830 TRACE [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at com.yq.controller.UserController.getUser(UserController.java:41)
trace rest get user=User(id=2, name=Tom2, [email protected], regDate=Sat Nov 10 15:23:18 CST 2018) by id=2

error1.txt內容為
2018-11-10 15:23:20,428 ERROR [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:19)
error LogbackApplication Start done.

控制檯的內容為
Starting application com.yq.LogbackApplication with URLs [file:/D:/E/workspaceGitub/springboot/LogbackDemo/target/classes/]
2018-11-10 15:23:20,428 INFO [DESKTOP-8S2E5H7 restartedMain] Caller+0 at org.springframework.boot.StartupInfoLogger.logStarted(StartupInfoLogger.java:57)
Started LogbackApplication in 6.774 seconds (JVM running for 7.439)
2018-11-10 15:23:20,428 ERROR [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:19)
error LogbackApplication Start done.
2018-11-10 15:23:20,428 WARN [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:20)
warn LogbackApplication Start done.
2018-11-10 15:23:20,428 INFO [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:21)
info LogbackApplication Start done.
2018-11-10 15:23:20,428 DEBUG [DESKTOP-8S2E5H7 restartedMain] Caller+0 at com.yq.LogbackApplication.main(LogbackApplication.java:22)
debug LogbackApplication Start done.
2018-11-10 15:23:26,749 DEBUG [DESKTOP-8S2E5H7 http-nio-9090-exec-1] Caller+0 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:149)
Initializing servlet ‘dispatcherServlet’