Springboot項目怎么處理日志-創(chuàng)新互聯(lián)

這篇文章主要講解了“Springboot項目怎么處理日志”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Springboot項目怎么處理日志”吧!

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比東洲網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式東洲網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋東洲地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

Springboot項目怎么處理日志Springboot項目怎么處理日志

如上圖,每天會生成一個新的日志文件,然后日志進行分類,我這里只對error和info進行分類。
怎么做呢?
首先,在resource目錄創(chuàng)建一個新文件,取名logback-spring.xml

Springboot項目怎么處理日志

<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>

    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>
                ERROR
            </level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--  滾動策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!--   路徑-->
            <fileNamePattern>
            /var/log/tomcat/sell/info.%d.log
            </fileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>
                ERROR
            </level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--  滾動策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--   路徑-->
            <fileNamePattern>
                /var/log/tomcat/sell/error.%d.log
            </fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />

    </root>
</configuration>

單例測試:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@Data
public class LoggerTest {

    @Test
    public void test1(){
        String name="laomi";
        String password="123456";
        log.info("debug.....");
        log.info("info.....");
        log.error("error....");
        log.info("name:{}, password:{}",name,password);

    }
}

添加依賴:

 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>test</scope>
 </dependency>

Springboot項目怎么處理日志

fileNamePattern這個是文件路徑,我是在這個項目的同級目錄找到新創(chuàng)建的文件夾的

Springboot項目怎么處理日志

感謝各位的閱讀,以上就是“Springboot項目怎么處理日志”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Springboot項目怎么處理日志這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!

當前文章:Springboot項目怎么處理日志-創(chuàng)新互聯(lián)
新聞來源:http://www.muchs.cn/article46/peieg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、面包屑導航網(wǎng)站策劃、小程序開發(fā)網(wǎng)站排名、軟件開發(fā)

廣告

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

成都做網(wǎng)站