基礎(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)
所以先來弄清楚這三個東東是什么
root
根logger寥院,也是一種logger,且只有一個level屬性
logger
用來設(shè)置某一個包或者具體的某一個類的日志打印級別以及指定appender
appender
負責寫日志的組件,PS粥航,我還沒看到詳細的娩贷,這里要補充
root logger appender三者的關(guān)系
root
是跟logger
管跺,所以他們兩是一回事锭亏,只不過root
中不能有name
和additivity
屬性咖城,只有一個level
appender
是一個日志打印的組件族檬,這個組件里面定義了打印過濾的條件歪赢、打印輸出方式、滾動策略单料、編碼方式埋凯、打印格式等。但它僅僅是一個打印組件扫尖,如果我們不適用一個logger
或者root
的appender-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.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.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
,ALL
和OFF
)祭犯,還有一個值INHERITED
或者同義詞NULL
秸妥,代表強制執(zhí)行上級的級別。如果沒有設(shè)置此屬性沃粗,那么當前logger
將會繼承上級的級別粥惧。addtivity:用來描述是否向上級
logger
傳遞打印信息。默認是true
最盅。
appender-ref
則是用來指定具體appender
的突雪。