logback

基礎(chǔ)概念logback.xml 文件結(jié)構(gòu)rootloggerappenderroot logger appender三者的關(guān)系實(shí)際項(xiàng)目實(shí)踐指定log文件輸出地址控制臺(tái)日志appender 配置詳解appender的種類filterrollingPolicy 子標(biāo)簽TimeBasedRollingPolicyfile 標(biāo)簽encoder 標(biāo)簽logger配置詳解

以知乎文章為基礎(chǔ)知乎logback 實(shí)際結(jié)合項(xiàng)目gridv為例

基礎(chǔ)概念

開始前先來一張圖比較好理解

logback.xml 文件結(jié)構(gòu)

img

所以先來弄清楚這三個(gè)東東是什么

root

根logger,也是一種logger,且只有一個(gè)level屬性

logger

用來設(shè)置某一個(gè)包或者具體的某一個(gè)類的日志打印級(jí)別以及指定appender

appender

負(fù)責(zé)寫日志的組件,PS,我還沒看到詳細(xì)的,這里要補(bǔ)充

root logger appender三者的關(guān)系

root是跟logger,所以他們兩是一回事,只不過root中不能有nameadditivity屬性,只有一個(gè)level

appender是一個(gè)日志打印的組件,這個(gè)組件里面定義了打印過濾的條件、打印輸出方式、滾動(dòng)策略、編碼方式、打印格式等。但它僅僅是一個(gè)打印組件,如果我們不適用一個(gè)logger或者rootappender-ref指定某個(gè)具體的appender時(shí),它就沒有任何用處的。

**因此appender讓程序知道怎么打、打印到那里、打印成怎么樣;而logger則是告訴程序哪些代碼可以這么打。即某個(gè)類下的代碼,可以使用這個(gè)appender打印

實(shí)際項(xiàng)目實(shí)踐

指定log文件輸出地址

文章中使用的是.properties文件,和我的項(xiàng)目.yml有點(diǎn)區(qū)別


文中設(shè)置 .properties和日志級(jí)別

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n7" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#設(shè)置應(yīng)用的日志級(jí)別
logging.level.com.glmapper.spring.boot=INFO

路徑

logging.path=./logs</pre>


項(xiàng)目中設(shè)置 .yml

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n10" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#路徑
spring:
logistics-log:
path: ./logs
?
logging:

設(shè)置log文件名稱命名,$是引用的意思

file: {spring.logistics-log.path}/{spring.application.name}.log

設(shè)置應(yīng)用的日志級(jí)別,info即代表info以上的都會(huì)打印出來,譬如error、warn

level:
root:info

設(shè)置log配置文件路徑,classpath即代表工程里的resources文件夾下,輸出里則在classes下

config: classpath:logback-spring.xml</pre>

所以就會(huì)輸出到項(xiàng)目根目錄下的logs

控制臺(tái)日志

文中的代碼

<pre mdtype="fences" cid="n24" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS} %-5level %logger{80} - %msg%n</Pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration></pre>

打印日志的源代碼

<pre mdtype="fences" cid="n28" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">private static final Logger LOGGER =
LoggerFactory.getLogger(HelloController.class);
@Autowired
private TestLogService testLogService;
?
@GetMapping("/hello")
public String hello(){
LOGGER.info("GLMAPPER-SERVICE:info");
LOGGER.error("GLMAPPER-SERVICE:error");
testLogService.printLogToSpecialPackage();
return "hello spring boot";
}</pre>

驗(yàn)證結(jié)果

<pre mdtype="fences" cid="n32" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">01:50:39.633 INFO com.glmapper.spring.boot.controller.HelloController

  • GLMAPPER-SERVICE:info
    01:50:39.633 ERROR com.glmapper.spring.boot.controller.HelloController
  • GLMAPPER-SERVICE:error</pre>

項(xiàng)目中的跟文中的區(qū)別不打,就不貼出來了

appender 配置詳解

appender有兩個(gè)屬性,name和class,name指定appender名稱,class指定appender的全限定名

以文中代碼為例

<pre mdtype="fences" cid="n105" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><appender name="GLMAPPER-LOGGERONE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>{logging.level}</level> </filter> <file>{logging.path}/glmapper-spring-boot/glmapper-loggerone.log
</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.path}/glmapper-spring-boot/glmapper-loggerone.log.%d{yyyy-MM-dd}</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender></pre>

appender的種類

  • ConsoleAppender:把日志添加到控制臺(tái)

  • FileAppender:文件

  • RollingFileAppender:滾動(dòng)記錄文件,先記錄日志到指定文件,當(dāng)符合某個(gè)條件時(shí),將日志記錄到其他文件。

filter

filter其實(shí)是appender里面的子元素。它作為過濾器存在,執(zhí)行一個(gè)過濾器會(huì)有返回DENY,NEUTRAL,ACCEPT三個(gè)枚舉值中的一個(gè)。

  • DENY:日志將立即被拋棄不再經(jīng)過其他過濾器

  • NEUTRAL:有序列表里的下個(gè)過濾器接著處理日志

  • ACCEPT:日志會(huì)被立即處理,不再經(jīng)過剩余過濾器

    ThresholdFilter

    臨界值過濾器,過濾掉低于指定臨界值的日志。當(dāng)日志級(jí)別等于或者高于臨街值時(shí),過濾器返回neutral,當(dāng)日志級(jí)別低于臨界值時(shí),日志被拒絕

    <pre mdtype="fences" cid="n143" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"><filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>INFO</level>
    </filter></pre>

    LevelFilter

    級(jí)別過濾器,根據(jù)日志級(jí)別進(jìn)行過濾。如果日志級(jí)別等于配置級(jí)別,過濾器會(huì)根據(jù)onMath(用于配置符合過濾條件的操作) 和 onMismatch(用于配置不符合過濾條件的操作)接收或拒絕日志。

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="sh" cid="n150" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"><filter class="ch.qos.logback.classic.filter.LevelFilter">
    <level>INFO</level>
    <onMatch>ACCEPT</onMatch>
    <onMismatch>DENY</onMismatch>
    </filter></pre>

rollingPolicy 子標(biāo)簽

這個(gè)子標(biāo)簽用來描述滾動(dòng)策略

TimeBasedRollingPolicy

最常用的滾動(dòng)策略,根據(jù)事件來滾動(dòng)

  • FileNamePattern

  • maxHistory

    <pre mdtype="fences" cid="n189" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;"><rollingPolicy
    class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

    <FileNamePattern>
    ${logging.path}/glmapper-spring-boot/glmapper-loggerone.log.%d{yyyy-MM-dd}
    </FileNamePattern>

    <MaxHistory>30</MaxHistory>
    </rollingPolicy></pre>

    上面的這段配置表明每天生成一個(gè)日志文件,保存30天的日志文件

file 標(biāo)簽

用于指定被寫入的文件名,可以使相對(duì)路徑或者絕對(duì)路徑,如果傷及目錄不存在就會(huì)自動(dòng)創(chuàng)建

<pre mdtype="fences" cid="n157" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><file>
${logging.path}/glmapper-spring-boot/glmapper-loggerone.log
</file></pre>

這個(gè)表示當(dāng)前appender將會(huì)將日志寫入到${logging.path}/glmapper-spring-boot/glmapper-loggerone.log這個(gè)目錄下

encoder 標(biāo)簽

對(duì)記錄事件進(jìn)行格式化,把日志信息轉(zhuǎn)換成字節(jié)數(shù)組,把字節(jié)數(shù)組寫入到輸出流

<pre mdtype="fences" cid="n167" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}

  • %msg%n</pattern>
    <charset>UTF-8</charset>
    </encoder></pre>

logger配置詳解

<pre mdtype="fences" cid="n197" lang="sh" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><logger name="com.glmapper.spring.boot.controller"
level="${logging.level}" additivity="false">
<appender-ref ref="GLMAPPER-LOGGERONE" />
</logger></pre>

上面的這個(gè)配置文件描述的是:com.glmapper.spring.boot.controller這個(gè)包下的${logging.level}級(jí)別的日志將會(huì)使用GLMAPPER-LOGGERONE來打印。logger有三個(gè)屬性和一個(gè)子標(biāo)簽:

  • name:用來指定受此logger約束的某一個(gè)包或者具體的某一個(gè)類。

  • level:用來設(shè)置打印級(jí)別(TRACE, DEBUG, INFO, WARN, ERROR, ALLOFF),還有一個(gè)值INHERITED或者同義詞NULL,代表強(qiáng)制執(zhí)行上級(jí)的級(jí)別。如果沒有設(shè)置此屬性,那么當(dāng)前logger將會(huì)繼承上級(jí)的級(jí)別。

  • addtivity:用來描述是否向上級(jí)logger傳遞打印信息。默認(rèn)是true

appender-ref則是用來指定具體appender的。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容