SpringBoot快速集成Logback日志組件
前言
在前一節(jié)的分享中,慕歌向大家介紹了如何使用spring boot 實(shí)現(xiàn)簡單的郵寄發(fā)送服務(wù),用于驗(yàn)證碼服務(wù)或者是通知服務(wù)。如果大家有興趣,慕歌還想向大家進(jìn)一步分享,如何在使用第三方服務(wù),實(shí)現(xiàn)手機(jī)短信通知服務(wù),就是那個(gè)我們每天都會(huì)使用到的短信驗(yàn)證碼,通知服務(wù)。這一節(jié)慕歌想帶來spring boot日志系統(tǒng)的分享,以及慕歌自己的實(shí)現(xiàn)的簡易日志記錄,慕歌會(huì)將日志同時(shí)保存在文件和數(shù)據(jù)庫之中。
引入:
如果我們使用 logback 就無需額外引入依賴,在spring boot statrt 中已經(jīng)默認(rèn)集成了logback 日志。
<!-- logback-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果想要使用log4j2 日志需要排除默認(rèn)日志,再引入,無需指定版本。
<!-- log4j2-->
<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>
<dependency>-->
<groupId>org.springframework.boot</groupId>-->
<artifactId>spring-boot-starter-log4j2</artifactId>-->
</dependency>-->
配置:
引入日志依賴后,我們還需要指定日志的輸出格式,需要再resource中新建一個(gè)文件 logback.xml 。如果對(duì)于日志無特殊要求可直接參考我的日志配置。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路徑 -->
<property name="log.path" value="/www/wwwroo/logs/"/>
<!-- 日志輸出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
<!-- 控制臺(tái)輸出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系統(tǒng)日志輸出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.log</file>
<!-- 循環(huán)政策:基于時(shí)間創(chuàng)建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的歷史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 過濾的級(jí)別 -->
<level>INFO</level>
<!-- 匹配時(shí)的操作:接收(記錄) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配時(shí)的操作:拒絕(不記錄) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.log</file>
<!-- 循環(huán)政策:基于時(shí)間創(chuàng)建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的歷史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 過濾的級(jí)別 -->
<level>ERROR</level>
<!-- 匹配時(shí)的操作:接收(記錄) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配時(shí)的操作:拒絕(不記錄) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 用戶訪問日志輸出 -->
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滾 daily -->
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的歷史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系統(tǒng)模塊日志級(jí)別控制 -->
<logger name="com.ewem" level="info"/>
<!-- Spring日志級(jí)別控制 -->
<logger name="org.springframework" level="warn"/>
<root level="info">
<appender-ref ref="console"/>
</root>
<!--系統(tǒng)操作日志-->
<root level="info">
<appender-ref ref="file_info"/>
<appender-ref ref="file_error"/>
</root>
<!--系統(tǒng)用戶操作日志-->
<logger name="sys-user" level="info">
<appender-ref ref="sys-user"/>
</logger>
</configuration>
這里的日志文件目錄由于是使用的linux ,配置格式就是從根目錄開始,需要換成你自己的日志輸出目錄。然后在配置中指定掃描路徑,使用我們D定義的日志格式:
# 日志配置
logging:
# 日志等級(jí)
level:
# 更換成你的目錄
channel.cert: info
org.springframework: warn
# 日志路徑
config: classpath:logback.xml
完成以下日志配置后,即可在我們開發(fā)的時(shí)候隨時(shí)打一個(gè)日志,用于錯(cuò)誤排查。
開發(fā):
那么在真實(shí)的開發(fā)中,我們?nèi)绾卧谝欢未a中引入日志呢。我們不妨可以將日志先看作最初學(xué)習(xí)的輸出語句??梢栽谌绾蔚胤捷敵鲆粭l信息。日志與標(biāo)準(zhǔn)輸出的不同在于,日志具有等級(jí):
- 【TRACE】:trace是一種很低的日志級(jí)別,一般不會(huì)使用。目前,我只有在SpringBoot的啟動(dòng)之中,略微發(fā)現(xiàn)一些它的影子,表示的就是默認(rèn)不打印的日志。
- 【DEBUG】:debug是一種調(diào)試程序的日志級(jí)別,一般用于程序開發(fā)過程中打印一些調(diào)試日志、運(yùn)行信息,可以比較隨意的使用。
- 【INFO】:info是用來輸出程序的一些關(guān)鍵信息,強(qiáng)調(diào)業(yè)務(wù)邏輯的運(yùn)行過程信息,不能隨便打印。
- 【W(wǎng)ARN】:warn是用于輸出一些警告提示信息,一般是系統(tǒng)進(jìn)入到一種可恢復(fù)的狀態(tài)時(shí)打印的信息,警告但不是嚴(yán)重錯(cuò)誤。
- 【ERROR】:error是系統(tǒng)已經(jīng)發(fā)生錯(cuò)誤的事件,比如發(fā)生了異常,但是不想影響系統(tǒng)的正常運(yùn)行。可以打印一些錯(cuò)誤信息,提示開發(fā)者關(guān)注或者定位問題。
日志是打印在日志文件之中的,如果大量的打印會(huì)造成日志文件的驟增導(dǎo)致磁盤空間快速增長。但是,排查問題的時(shí)候,我們都盡可能希望日志級(jí)別可以足夠的細(xì)致。沒有問題的時(shí)候,我們希望日志文件可以盡量減少磁盤的占用。所以,如果我們可以做到動(dòng)態(tài)地去控制日志級(jí)別,實(shí)現(xiàn)動(dòng)態(tài)打印日志,那就可以完美解決上訴的需求。
/**
* 管理員
*/
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminUserController {
@Autowired
AdminUserService adminUserService;
@Autowired
AdminLogService adminLogService;
//用戶詳情
@SaCheckLogin
@RequestMapping("/searchOne")
public R searchOne(@RequestBody AdminUser adminUser){
AdminUser user = adminUserService.getById(adminUser.getId());
log.trace("info"+user);
log.info("info"+user);
log.debug("info"+user);
log.info("info"+user);
log.warn("info"+user);
return R.success(user);
}
}
在使用日志的時(shí)候不直接指定由logback 進(jìn)行日志記錄,而使用slf4j 上層接口,進(jìn)行日志的寫入。這樣即使,我們更換了日志系統(tǒng),并不會(huì)導(dǎo)致代碼中使用了舊日志系統(tǒng)的日志點(diǎn)報(bào)錯(cuò)。
結(jié)語
到此這篇關(guān)于SpringBoot快速集成Logback日志組件的文章就介紹到這了,更多相關(guān)SpringBoot集成Logback日志組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis 動(dòng)態(tài)SQL的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis 動(dòng)態(tài)SQL的幾種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例
本文主要介紹了SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例,只需創(chuàng)建實(shí)體類和mapper接口,就可以實(shí)現(xiàn)單表的增刪改查操作,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
eclipse實(shí)現(xiàn)ECDSA數(shù)字簽名
這篇文章主要為大家詳細(xì)介紹了eclipse實(shí)現(xiàn)ECDSA數(shù)字簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Spring Security自定義登錄原理及實(shí)現(xiàn)詳解
這篇文章主要介紹了Spring Security自定義登錄原理及實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
mybatis中如何傳遞單個(gè)String類型的參數(shù)
這篇文章主要介紹了mybatis中如何傳遞單個(gè)String類型的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用springboot實(shí)現(xiàn)郵箱發(fā)送(激活碼)功能,文中的示例代碼簡潔易懂,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10

