logback

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

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

基礎(chǔ)概念

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

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

img

所以先來弄清楚這三個東東是什么

root

根logger寥院,也是一種logger,且只有一個level屬性

logger

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

appender

負責寫日志的組件,PS粥航,我還沒看到詳細的娩贷,這里要補充

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

root是跟logger管跺,所以他們兩是一回事锭亏,只不過root中不能有nameadditivity屬性咖城,只有一個level

appender是一個日志打印的組件族檬,這個組件里面定義了打印過濾的條件歪赢、打印輸出方式、滾動策略单料、編碼方式埋凯、打印格式等。但它僅僅是一個打印組件扫尖,如果我們不適用一個logger或者rootappender-ref指定某個具體的appender時白对,它就沒有任何用處的。

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

實際項目實踐

指定log文件輸出地址

文章中使用的是.properties文件悦污,和我的項目.yml有點區(qū)別


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

<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)用的日志級別
logging.level.com.glmapper.spring.boot=INFO

路徑

logging.path=./logs</pre>


項目中設(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)用的日志級別屈溉,info即代表info以上的都會打印出來塞关,譬如error、warn

level:
root:info

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

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

所以就會輸出到項目根目錄下的logs

控制臺日志

文中的代碼

<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>

驗證結(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>

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

appender 配置詳解

appender有兩個屬性线梗,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:把日志添加到控制臺

  • FileAppender:文件

  • RollingFileAppender:滾動記錄文件仪搔,先記錄日志到指定文件瘾婿,當符合某個條件時,將日志記錄到其他文件烤咧。

filter

filter其實是appender里面的子元素偏陪。它作為過濾器存在,執(zhí)行一個過濾器會有返回DENY煮嫌,NEUTRAL笛谦,ACCEPT三個枚舉值中的一個。

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

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

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

    ThresholdFilter

    臨界值過濾器饥脑,過濾掉低于指定臨界值的日志。當日志級別等于或者高于臨街值時懦冰,過濾器返回neutral灶轰,當日志級別低于臨界值時,日志被拒絕

    <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ù)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 子標簽

這個子標簽用來描述滾動策略

TimeBasedRollingPolicy

最常用的滾動策略伴澄,根據(jù)事件來滾動

  • 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>

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

file 標簽

用于指定被寫入的文件名瓤鼻,可以使相對路徑或者絕對路徑,如果傷及目錄不存在就會自動創(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>

這個表示當前appender將會將日志寫入到${logging.path}/glmapper-spring-boot/glmapper-loggerone.log這個目錄下

encoder 標簽

對記錄事件進行格式化贤重,把日志信息轉(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>

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

  • name:用來指定受此logger約束的某一個包或者具體的某一個類并蝗。

  • level:用來設(shè)置打印級別(TRACE, DEBUG, INFO, WARN, ERROR, ALLOFF)祭犯,還有一個值INHERITED或者同義詞NULL秸妥,代表強制執(zhí)行上級的級別。如果沒有設(shè)置此屬性沃粗,那么當前logger將會繼承上級的級別粥惧。

  • addtivity:用來描述是否向上級logger傳遞打印信息。默認是true最盅。

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市涡贱,隨后出現(xiàn)的幾起案子咏删,更是在濱河造成了極大的恐慌,老刑警劉巖问词,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件督函,死亡現(xiàn)場離奇詭異,居然都是意外死亡激挪,警方通過查閱死者的電腦和手機辰狡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垄分,“玉大人宛篇,你說我怎么就攤上這事》嫦玻” “怎么了些己?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長嘿般。 經(jīng)常有香客問我段标,道長,這世上最難降的妖魔是什么炉奴? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任逼庞,我火速辦了婚禮,結(jié)果婚禮上瞻赶,老公的妹妹穿的比我還像新娘赛糟。我一直安慰自己,他們只是感情好砸逊,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布璧南。 她就那樣靜靜地躺著,像睡著了一般师逸。 火紅的嫁衣襯著肌膚如雪司倚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機與錄音动知,去河邊找鬼皿伺。 笑死,一個胖子當著我的面吹牛盒粮,可吹牛的內(nèi)容都是我干的鸵鸥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼丹皱,長吁一口氣:“原來是場噩夢啊……” “哼妒穴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起种呐,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤宰翅,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后爽室,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體汁讼,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年阔墩,在試婚紗的時候發(fā)現(xiàn)自己被綠了嘿架。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡啸箫,死狀恐怖耸彪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情忘苛,我是刑警寧澤蝉娜,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站扎唾,受9級特大地震影響召川,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜胸遇,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一荧呐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纸镊,春花似錦倍阐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至凯旭,卻和暖如春概耻,著一層夾襖步出監(jiān)牢的瞬間楣颠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工咐蚯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人弄贿。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓春锋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親差凹。 傳聞我的和親對象是個殘疾皇子期奔,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容