springboot学完本篇不要再说你不会logback
一 前言
知识追寻者springboot系列之logback,觉得文章不错,点赞加重视谢谢;重视公众号知识追寻者获取更多精彩原创内容;
logback是一个成熟的log4j 工程,由 Ceki Glc 所创造,也c $ u v是 log4j 日. i % [志框架的创建者;
spri/ 6 $ |ngboot默许运用的日志框架是
logback,其由三e & G V F H s p个组件组成
- logback-core
- log& } p , (back-classic
- logback-access
l] Q ) /ogback-spring.xml 文件放在classpu , 0 v Lath (resource目录)下 即可自动加载
二logback 基本特点
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logba! ~ ` ack.core.ConsoleAppender">
<!-- encoders are by defau! & ~lt assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoderP ! K >
</appender>
<root level="debc F H bug">
<appender-ref ref="STDOUT" />
</ro& ` e V 0 r tot>
&lO @ xt;/configuration>
日志格局说明
- %d 表明日期时刻
- %thread表明线程名
- %K $ m / a s i { ,-5level:等级从左显现5个字符宽度
- Loggerj B S ~ w R q :一般运用源代码的类名
- %msg:日志音讯
- %n是换行符
有用的日志等级如下
level of request p | ||||||
---|---|---|---|---|---|---|
TRACE | DEBUG | INFO | WARN | ERROR | OFF | |
TRACE | YES | NO | NO | NO | NO | NO |
DEBUG | YES | YES | NO | NOi _ } X | NO | NO |
INFO | YES | YES | YES | NO | NO | NO |
WARN | YES | YES | YES | YES | NO | NO |
ERROR | YES | YES | YES | YES | YES | NO |
2.1 configuration 标签特点
- scan : 装0 C ] { i a备文件假如发作改动,将会被重新加载,默许值为true
<configuration debug="true">
...
</configuration>
- debug: 实时查看logback运转状况,默许值为false
<configuration scan="true">
...
</configuration>
- scanPeriod:Q k P @ W g K _ 监测装备文件是否有修改的时刻距离, 默许 每分钟M h k x 6 z;读者能够设置 示例
30 seconds
,30 minutes
,3 hours
<configuration scan="tZ c a O -rue" scanPeriod="30 s] ; q ( / Oe2 a = 2conds" >
...
&l A S & d . ut;/configuration>4 % _ f & p e
- packagingData : 堆栈盯梢中是否启用打包数据,默许false;
<configC ; [ I 2 @ v uration packagingr [ # 8 f B jData="true"G g H % Z H c>
...
</con, o I -figuration>[ l Y c A
2.2 statusListeG V Q g $ner 标签
statusListener
为configuration 的子元素。称为状况监听器,在 configuration 的Z | I p r G 7 ~子标签顶层,意指监听事情;
<configuration>
<statusListener class="ch.qos.logback.core.statusI q q h t J. [ O N f 4 ^OnConsoleStatusListener* h L t } M" />
... the rest of the configup o o ^ g 3 Jration file
</configuration>
2.3 property标b $ b签
特] , d h ~ ] ~点 name , value 用来界说变量的 称号 和值 ,在上下文中能够经过 ${name} 的方式进行调R X D F { @ l用
<configuration>
<property name="USER_HOME" value="/home/sebastien" />
<appender name="FILE" class="ch.qos.lod @ u u : 4 I Kgback.core.A b T , g *FileAppend? K & Qer">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<pattern>%w 4 Gmsg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
假如 界说如下示例会去 variaD B t m ` U F Dbles1.properties 中读取装备信息
<configuration>
<property file=j { ."src/main/java/c~ d y % ? +hapters3 . 8 K H/configuration/variables1.properties" />
&[ l { #lt;appender name="FILE" class="ch.qos.logback.4 j k a ^ d 0core.FileAppender">
<file>${USER_HOME}/myApp.log</file>
<encoder>
<patteC y rn>%msg%n</pattern>
</en/ @ y +co@ 1 X 9 G _ F N gder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
&l@ d x U Jt;/configuration>
variables1.properties:
USER_HOME=/home/sebastien
2.4{ [ { 3 p 2 K – !appender标签
appender
为configuration 的子元素,每个 appender
都是一个日志组件, 能够界说一种类型的日志;
-
name
:appender 的称号,该值首要用于ref
。 -
class
:界说appender 组件。 -
scope
:指定作用域 ;LOCAL
,CONTEXT
,SYSTEM
如下示例 : 界说2 个组件, 一个是% 6 : C | 6 g文件存储,一个控制输出,经过 root 标签引用即可同时生效;
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileApp] B =ender">
<file>myApp.loV ~ P . s % o ig</file>
&w Z [lt;encoder>
<p+ 5 c t a pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</ency z t ` & y T - poder>
</appe@ 7 ) 6 Q V D qnder>
<appender name="STDO[ q ~ V ] g oUT" class="ch.qos.logback.core.ConsoleAppG m F _ ? O ? g eender">
<encoder>Y b |;
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<a( T b J ! kppender-8 R ; E V ] ? !ref ref="FILE"u 7 7 c - z H />
<ap! q 6 ] h pender-ref ref="STDOUT" />
</root>
</configuratM v z C V # kion>
2.5contextName标签
contextName
configuration 的子元素。每一个logger 都能够绑定一个contextName
,默许上下文称号为 default , 假如设定完成,则不能改动;
<confS _ c = m u ^iguB H T & # ! o h #ration>
<contextName>myAppName</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppens , g Z Ader">
<encoder>
<pattern>%d %cont, x J E AextName [%t] %level %lou B l ( ] W @ ogger{36} - %msg%n</pattU e ] ] L s dern>
</encoder>
</appn q ( ) 6 Y G - ender>
<b z Proot level="debug">
&| - L u ^ ~ x G Zlt;appender-ref ref="STDOUT" />} e F;
</root>
</configuration>
2.6 logger标签
用来设置某个包或及具体的某个类的日志输出以及指定 <appender>
; name 特点一个, level,addtivity(是否向上级loger传递打印信息) 特点可选
如下所示,不想看见包chapters.configuration 中的debug等级日志,能够进行如下装备
<configuration>
<appender name="STDOUT" class="ch.qos.logbB q D U 4 / M vack.core.ConsoleAppender">
<!-- encodeA # ; ~ k 5 $ Yrs are assigned the type
ch.qoB ) ]s.logback.classic.encoder.PattX 3 S K 3 i | bernLayoutEncodx I C Jer by default -->p ] N / 8 _ y r
<encoder>
<pattern>%d{Ho ? 3 GH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
&# v , o o W Vlt;/encoder>
</appender>
<logger name="chapters.configuration" level="INFO"/>
<!-- Strictly speaking, the level attribute is not necessaG n z N Wry since -->
&l i i r h F N P #t;!-- the level of the root level is set to DEBUG by default. -->
<root levelS ( e 2 9 ` M @ O="DEBUG">
<appender-ref ref="STDO& 1 L = 9 l J !UTV - l 1 @ _ ! ~ l" />
</root>
</configuration>
打印如下
17:34:07.578 [main] INFO chapters.configurationM f j f 6.MyApp3 - Entering application.
17:34:07.5L i @ / q l78 [main] INFO chapters.configuration.MyApp3 - Exiting al x i & 0pplication.
2.| m j R # 1 x ^ =7 root 标签
root标签本质是&lV 8 ~ n 9 )t;logg. $ q + 3 ` P }er>
标签,不过其是根标签;若 <logger >@ @ Y / ;
或 <aC 1 1 = - , 5ppender>
标签为设置输出等级时就会默许承继该标签设置的等级!
<!-- 日志输出等级 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FU 0 & 2 YILE" />
</rE x c ]oot>
2.8 include标z | m t m ; y签
包括其它文件的装备信息Y 4 0 1 t z t , |
<configuratW I . ; Pion>
<include file="src/main/java/8 V n ) %chapters/configuration/includedConfig.xml"/>m % ^ C F 8 R $
<root level="DEBUG">
<appende{ M N ) R 2 nr-ref ref="includedConsole" />
</root>
</conf. = 6 a yiguration>
includedConfig.xml 示例,必须包括<inclur P Q b % Dded>
标签
&! S e ( v x X ] 3lt;included>
<appe/ e | , q Y $ t vnder name="includ G i { KedConsole" class="ch.qos.logback.core.ConsoleJ n 2 [ ~ 5 m kAppender"3 / % f : m 8>P G : M S L V $ Q
&lT x ( } M d it;enco} s N [der>
<pattern>"%d - %m%n"</pattern>
</encoder>
</appender>
</included>
如过是URL
<incO ; ) Y v 7lude url="http://some.hog ) K ` 3 T S 5st.com/includedConfig.xml"/>
三 多环境装备
为了f W d支撑 development, testing 和 production 多环境下logback 不抵触的问题,o H N p t p能够运用 <if>
, then
进行装备,使目标环境生效;
格局如下
<!-- if-then form -->
<iP J x u E / f Wf condity + d 4 , 4ion="some conditional eR - uxpression">
<then>
...
</then>
</if>
<!-- if-then-e{ * ~lse form -->
<if condition="some cons ~ o C C rdf m : a A 8 D Aitional expression">
<then&g, % B 3 A : et;
...
</then>
<else>
...
</else>
</if>
四 示例
4.1正常日志appender示例
<!-- 日志 appender : 依照每天生成日志文件 -->
<appender name; ^ I 7 l a c /="NORMAL-FILE" class="ch.qos.logback.cf - Vore.rold # _ |ling.RollingFileAppender"&1 N } , xgt;
<append>true</append>
<!-- 日D F n T j W志称号 -->
<file>${logging.pR ^ O /ath}/zszxz-boot/zszxz-error.log</file>
<!-- 每天生m | ~ [ P ; w b成一个日志文件,保存30天的日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollinM $ * -gPolicy">
<!--日志文件输出的文件名:按天回滚 daily -->
<Fi& 9 z F WleNamePattern>${lo| I , x k x f f !gging.path}/zszxz-boot7 ~ S/zszxz.Z E = : . blog.%d{yyyy-MM-dd}</FileNam0 # R f UePattern>
<!_ : y--日志文件保存天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ci x ? rh.qos.logX 5 r Pback.classic.encoder.PatternLayoutEncoder">
<!--格局化输出:%d表明日期,%thread表明线程名,%-5level:% d x k F l等级从左显现5个字符宽度%msg:日志音讯,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%threy k d u s wad] %-5level %logger{50} - %msO 8 E 8 q { Qg%n</pattern>
<!-- 编码B b U F o B I -->
<charset>UTF-8</charset>
</encoder>
</appender>
4.2错误日志appender示例
<!-- 错误日志 appeg v * A Onder : 依照每天生成日志文件 -->
<appendZ l 5 7 L W N ? Der name="ERROR-FILE" class="ch.qos.logback.core.rolling.RollingFileAppeU i ` u 9 . qnder">
<append>true</append>
<!-- 过滤器,只记载 error 等级的日E L x p Y j志 -->
<filter class="ch.qos.logC f ] R 2 S d Mb/ b 8 f M [ Tack.classic.filter.ThresholdFilter">
<level>error</level>
</filte[ v x x O ` 1 ? 2r>
<!-- 日志称号 -->
<file>${logging.path_ 0 y R F j}/zszxz-booW X X ; e Zt/zszxz-error.log</file>
<!-- 每天生成一个日志文件,保存30天的日志文件 -->F v };
<rollingPolicy class="ch.qos.logbacA S ck.core.rolling.TimeBasedRollingPolic. R Z Xy">
<!--日志文件输出的文件名:按天回滚 daily -->
<FileNamePattern>${loggi` ( ]ng.path}/zszxz-boot/zszxz-error.log.%d{yyyy-MM-dd}</Filec Z 2 [ / /NamePattern>
<!--日志文件保存天数-->
<MaxHis M B =toK B bry>30</MaxHistory>
</rollingPolicyw % ! . E>
<encoder class="ch.qos.logbac/ t D S X Pk.classicH u y Q h I |.encoder.PatternLayoutEncoder">
<!--格局化输出:%d表明日期,%threan # % ( W Kd表明线程名,%-i 1 w ;5level:等U ; B u 5 Y =级从左显现5个字符宽度%msg:日志音j _ L v &讯,%n是换行符-->c 1 .;
<pattern>%d{yyyy-MM-dd HH:mm:ssn G } ! : : 5.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<!-- 编码 -->
<charset>UP M Z t ~ v S yTF-8&l( 4 ^ 8 2 = s (t;/charset>
</encoder>
</appender>
五 官方文_ i ) m G E 5 x档
更多的日志装备内容请参照官方文档
logback.qos.ch/manual/inde^ J v 4…