1. 程式人生 > >SpringBoot 整合Log4j2

SpringBoot 整合Log4j2

1.先引用驅動包

 <!--log4j2配置依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

     注意! Spring Boot引用log4j-core ,和log4j-slf4j-impl會報如下錯誤!!!!

     Logback配置錯誤:

2.匯入log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--設定log4j2的自身log級別為DEBUG-->
<configuration status="DEBUG ">
    <!--日誌級別以及優先順序排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->

    <!--All:最低等級的,用於開啟所有日誌記錄.
        Trace:是追蹤,就是程式推進以下,你就可以寫個trace輸出,所以trace應該會特別多,不過沒關係,我們可以設定最低日誌級別不讓他輸出.
        Debug:指出細粒度資訊事件對除錯應用程式是非常有幫助的.
        Info:訊息在粗粒度級別上突出強調應用程式的執行過程.
        Warn:輸出警告及warn以下級別的日誌.
        Error:輸出錯誤資訊日誌.
        Fatal:輸出每個嚴重的錯誤事件將會導致應用程式的退出的日誌.
        OFF:最高等級的,用於關閉所有日誌記錄.
       程式會列印高於或等於所設定級別的日誌,設定的日誌等級越高,打印出來的日誌就越少。-->
    <appenders>
        <!-- 控制檯輸出 -->
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %n%msg%n"/>
        </console>

        <!--檔案會打印出所有資訊,這個log每次執行程式會自動清空,由append屬性決定,這個也挺有用的,適合臨時測試用-->
        <File name="log" fileName="log/test.log" append="true">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>

        <!-- 這個會打印出所有的info及以下級別的資訊,每次大小超過size,則這size大小的日誌會自動存入按年份-月份建立的資料夾下面並進行壓縮,作為存檔-->
        <RollingFile name="RollingFileInfo" fileName="D:\\log4j2\\allOut.log"
                     filePattern="D:\\log4j2\\$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log.gz">

            <!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>

            <!-- 輸出格式 -->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggerzingPolicy interval="1"/>
                <!-- SizeBasedTriggeringPolicy單個檔案的大小限制 -->
                <SizeBasedTriggeringPolicy size="2 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy屬性如不設定,則預設為最多同一資料夾下7個檔案,這裡設定了20 -->
            <!-- DefaultRolloverStrategy同一個檔案下的最大檔案數 -->
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>

    </appenders>

    <!--過濾掉spring和hibernate(或者mybatis)的一些無用的debug資訊-->
    <loggers>
        <logger name="org.springframework" level="INFO"></logger>
        <!--(name=orj.hibernate或者name=orj.mybatis)-->
        <logger name="org.mybatis" level="INFO"></logger>
        <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
        </root>
    </loggers>

</configuration>

3.配置application.yml

#配置log4j日誌管理
logging:
  config: classpath:log4j2.xml

4.呼叫log4j2的方法

 private static final Logger logger= LogManager.getLogger();
 //列印日誌
 logger.info(i);
 logger.debug(i);

  到此Spring boot關於log4j2的教程就到這裡啦。