這篇文章主要講解了“Springboot項(xiàng)目怎么處理日志”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Springboot項(xiàng)目怎么處理日志”吧!
如上圖,每天會(huì)生成一個(gè)新的日志文件,然后日志進(jìn)行分類,我這里只對(duì)error和info進(jìn)行分類。
怎么做呢?
首先,在resource目錄創(chuàng)建一個(gè)新文件,取名logback-spring.xml
<?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> <!-- 滾動(dòng)策略--> <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> <!-- 滾動(dòng)策略--> <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>
單例測(cè)試:
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>
fileNamePattern這個(gè)是文件路徑,我是在這個(gè)項(xiàng)目的同級(jí)目錄找到新創(chuàng)建的文件夾的
感謝各位的閱讀,以上就是“Springboot項(xiàng)目怎么處理日志”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Springboot項(xiàng)目怎么處理日志這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
當(dāng)前文章:Springboot項(xiàng)目怎么處理日志-創(chuàng)新互聯(lián)
新聞來源:http://chinadenli.net/article46/peieg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、網(wǎng)站策劃、小程序開發(fā)、網(wǎng)站排名、軟件開發(fā)
聲明:本網(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)
猜你還喜歡下面的內(nèi)容