SpringBoot怎么進(jìn)行日志處理

這篇文章主要講解了“Spring Boot怎么進(jìn)行日志處理”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Spring Boot怎么進(jìn)行日志處理”吧!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供定遠(yuǎn)網(wǎng)站建設(shè)、定遠(yuǎn)做網(wǎng)站、定遠(yuǎn)網(wǎng)站設(shè)計(jì)、定遠(yuǎn)網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、定遠(yuǎn)企業(yè)網(wǎng)站模板建站服務(wù),10余年定遠(yuǎn)做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

? Log4j2 性能

  • https://logging.apache.org/log4j/2.x/performance.html

? Spring Boot 依賴與配置

Maven 依賴

<!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 日志 Log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <!-- Log4j2 異步支持 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.3.6</version>
        </dependency>

XML 配置 resources/log4j2.xml

  • 混合 sync/async

  • 彩色日志

  • 分類輸出到不同文件

  • 自動(dòng)壓縮日志文件并歸檔

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration后面的status,這個(gè)用于設(shè)置log4j2自身內(nèi)部的信息輸出,可以不設(shè)置,當(dāng)設(shè)置成trace時(shí),
     你會(huì)看到log4j2內(nèi)部各種詳細(xì)輸出??梢栽O(shè)置成OFF(關(guān)閉) 或 Error(只輸出錯(cuò)誤信息)。
     30s 刷新此配置
-->
<configuration status="WARN" monitorInterval="30">

    <!-- 日志文件目錄、壓縮文件目錄、日志格式配置 -->
    <properties>
        <Property name="fileName">/Users/admin/Code/log</Property>
        <Property name="fileGz">/Users/admin/Code/log/7z</Property>
        <Property name="PID">????</Property>
        <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
    </properties>

    <Appenders>
        <!-- 輸出控制臺(tái)日志的配置 -->
        <Console name="console" target="SYSTEM_OUT">
            <!--控制臺(tái)只輸出level及以上級(jí)別的信息(onMatch),其他的直接拒絕(onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <!-- 輸出日志的格式 -->
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>

        <!-- 打印出所有的信息,每次大小超過size,則這size大小的日志會(huì)自動(dòng)存入按年份-月份建立的文件夾下面并進(jìn)行壓縮,作為存檔 -->
        <RollingRandomAccessFile name="infoFile" fileName="${fileName}/web-info.log" immediateFlush="false"
                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-info.gz">
            <PatternLayout pattern="${LOG_PATTERN}"/>

            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>

            <Filters>
                <!-- 只記錄info和warn級(jí)別信息 -->
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <!-- 指定每天的最大壓縮包個(gè)數(shù),默認(rèn)7個(gè),超過了會(huì)覆蓋之前的 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingRandomAccessFile>

        <!-- 存儲(chǔ)所有error信息 -->
        <RollingRandomAccessFile name="errorFile" fileName="${fileName}/web-error.log" immediateFlush="false"
                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-error.gz">
            <PatternLayout pattern="${LOG_PATTERN}"/>

            <Policies>
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>

            <Filters>
                <!-- 只記錄error級(jí)別信息 -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <!-- 指定每天的最大壓縮包個(gè)數(shù),默認(rèn)7個(gè),超過了會(huì)覆蓋之前的 -->
            <DefaultRolloverStrategy max="50"/>
        </RollingRandomAccessFile>
    </Appenders>

    <!-- Mixed sync/async -->
    <Loggers>
        <Root level="debug" includeLocation="true">
            <AppenderRef ref="console"/>
            <AppenderRef ref="infoFile"/>
            <AppenderRef ref="errorFile"/>
        </Root>

        <AsyncRoot level="debug" includeLocation="true">
            <AppenderRef ref="console"/>
            <AppenderRef ref="infoFile"/>
            <AppenderRef ref="errorFile"/>
        </AsyncRoot>
    </Loggers>

</configuration>

最終效果如下:

Spring Boot怎么進(jìn)行日志處理

問題來了,請(qǐng)問諸位你們項(xiàng)目中目前在使用Logback還是Log4j2呢?

感謝各位的閱讀,以上就是“Spring Boot怎么進(jìn)行日志處理”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Spring Boot怎么進(jìn)行日志處理這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)站名稱:SpringBoot怎么進(jìn)行日志處理
標(biāo)題路徑:http://muchs.cn/article20/johgco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃服務(wù)器托管、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、動(dòng)態(tài)網(wǎng)站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)