溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring boot中如何自定義日志 log4j2

發(fā)布時(shí)間:2021-07-29 15:33:59 來源:億速云 閱讀:167 作者:Leah 欄目:大數(shù)據(jù)

spring boot中如何自定義日志 log4j2,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

1 修改spring-boot-starter的dependency

 <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>

添加我們需要自定義的logging的dependency,這里用的是log4j2

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
2 自定義配置文件log4j2.xml
  • Spring boot對(duì)自定義配置文件的名稱是有要求的,對(duì)Login4j2而言必須為log4j2-spring.xml or log4j2.xml

  • 關(guān)于配置文件中的參數(shù),詳細(xì)參考官方文檔

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appenders>
        <!-- 控制臺(tái)輸出 -->
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class %L %M - %msg%n"/>
        </console>

        <!-- fileName:輸出路徑  filePattern:命名規(guī)則 -->
        <RollingFile name="all" fileName="logs/allOut.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="all" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 輸出格式 -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%n"/>
            <Policies>
                <!-- SizeBasedTriggeringPolicy單個(gè)文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="2 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一個(gè)文件下的最大文件數(shù) -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>

        <RollingFile name="err" fileName="logs/err.log"
                     filePattern="logs/$${date:yyyy-MM-dd}/err-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <!-- 輸出格式 -->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <Policies>
                <!-- SizeBasedTriggeringPolicy單個(gè)文件的大小限制 -->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy同一個(gè)文件下的最大文件數(shù) -->
            <DefaultRolloverStrategy max="50"/>
        </RollingFile>
    </appenders>

    <loggers>
        <!--過濾掉spring無用的debug信息-->
        <logger name="org.springframework" level="error"></logger>

        <root level="debug">
            <appender-ref ref="Console"/>
            <appender-ref ref="all"/>
            <appender-ref ref="err"/>
        </root>
    </loggers>

</configuration>

關(guān)于spring boot中如何自定義日志 log4j2問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI