กรอบงานการบันทึก Logback.xml คืออะไร ConsoleAppender, FileAppender และ RollingFileAppender Example
เผยแพร่แล้ว: 2019-02-13
คุณรู้จัก Logback Logging Framework
หรือไม่ คนส่วนใหญ่ใช้ log4j and slf4j
ในโครงการของพวกเขาเป็นหลัก ตั้งแต่ 6 เดือนที่ผ่านมา ฉันใช้เฟรมเวิร์กการบันทึก Logback.xml
และฉันจะบอกว่าฉันเห็นการปรับปรุงประสิทธิภาพค่อนข้างน้อยในแอปพลิเคชันที่ใช้งานจริงของฉัน
ในบทช่วยสอนนี้ เราจะพูดถึงตัวอย่างเชิงปฏิบัติของเฟรมเวิร์กการบันทึก Logback
logback.qos.ch คืออะไร?
Logback เป็นเวอร์ชันใหม่ของ Log4j ได้รับการพัฒนาจากการหยุดการพัฒนา log4j

บน Crunchify เราได้เผยแพร่บทช่วยสอนหลายรายการใน Log4j หากคุณสนใจ:
- วิธีตั้งค่าระดับการบันทึก log4j ของคุณเอง
- จะกำหนดค่า log4j.properties ได้อย่างไร?
เราจะพูดถึงขั้นตอนโดยละเอียดทั้งหมดเกี่ยวกับวิธีตั้งค่า Logback Logging Framework สำหรับโครงการ Enterprise Java ของคุณ เราจะสร้างไฟล์ใหม่ด้านล่าง
- ภายใต้โฟลเดอร์ทรัพยากรไฟล์
logback.xml
- Java Class
CrunchifyLogbackLogging.java
มาเริ่มกันเลย
ขั้นตอนที่ 1 สร้างไฟล์ logback.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<? xml version = "1.0" encoding = "UTF-8" ?> < configuration > < appender name = "CRUNCHIFYOUT" class = "ch.qos.logback.core.ConsoleAppender" > < layout class = "ch.qos.logback.classic.PatternLayout" > < Pattern > % d { HH : mm : ss . SSS } [ % thread ] % - 5level % logger { 36 } % X { sourceThread } - % msg % n < / Pattern > < / layout > < / appender > < appender name = "CRUNCHIFYFILE" class = "ch.qos.logback.core.FileAppender" > < file > / Users / ashah / Documents / crunchify . log < / file > < encoder > < pattern > % date % level [ % thread ] % logger { 10 } [ % file : % line ] % msg % n < / pattern > < / encoder > < / appender > < appender name = "CRUNCHIFYROLLING" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file > / Users / ashah / Documents / crunchify - rolling . log < / file > < encoder class = "ch.qos.logback.classic.encoder.PatternLayoutEncoder" > < Pattern > % d { yyyy - MM - dd HH : mm : ss } [ % thread ] % level % logger { 35 } - % msg % n < / Pattern > < / encoder > < rollingPolicy class = "ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > < ! -- We are rolling over daily -- > < fileNamePattern > crunchify . log . timeroll . % d { yyyy - MM - dd } . log < / fileNamePattern > < ! -- keep 10 days ' worth of history capped at 1GB total size -- > < maxHistory > 10 < / maxHistory > < totalSizeCap > 1GB < / totalSizeCap > < / rollingPolicy > < / appender > < ! -- default is DEBUG -- > < root level = "DEBUG" > < appender - ref ref = "CRUNCHIFYOUT" / > < appender - ref ref = "CRUNCHIFYFILE" / > < appender - ref ref = "CRUNCHIFYROLLING" / > < / root > < / configuration > |
มาทำความเข้าใจไฟล์ logback.xml กันก่อน
- ที่ด้านล่างของไฟล์ คุณจะเห็นว่าเรามี 3 appender อ้างอิง
appender-ref
ภายใต้องค์ประกอบroot
- เรากำลังใช้ระดับบันทึกเป็น
DEBUG
นั่นหมายถึงพิมพ์ทุกอย่างด้วย DEBUG ระดับขึ้นไป
CRUNCHIFYOUT
appender เป็นคลาส ch.qos.logback.core ConsoleAppender
ซึ่งหมายความว่า ข้อมูลการบันทึกทั้งหมดจะถูกพิมพ์ไปยัง Eclipse Console
CRUNCHIFYFILE
เป็นประเภท ch.qos.logback.core FileAppender
นั่นหมายความว่า ข้อมูลการบันทึกทั้งหมดจะถูกพิมพ์ไปยังไฟล์ในเครื่อง ในกรณีของเราคือ /Users/ ashah /Documents/ crunchify.log
CRUNCHIFYROLLING
เป็นประเภท ch.qos.logback.core.rolling RollingFileAppender
. ดังที่คุณเห็นใน rollingPolicy เรากำลังกลิ้งไฟล์ทุกวันและเก็บไฟล์ได้สูงสุด 10 ไฟล์ นอกจากนี้ยังมีขีดจำกัดความจุของขนาดไฟล์ทั้งหมด ซึ่งเท่ากับ 1 GB
ในตัวอย่างของเรา ไฟล์ใหม่จะถูกสร้างขึ้นที่ตำแหน่ง /Users/ ashah /Documents/ crunchify -rolling.log
ขั้นตอนที่ 2 เขียนโปรแกรม Java เพื่อทดสอบ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package crunchify . com . tutorial ; import org . slf4j . Logger ; import org . slf4j . LoggerFactory ; /** * @author Crunchify.com * Program: Very simple Logback.xml test class with ConsoleAppender, FileAppender and RollingFileAppender * Version: 1.0.0 * */ public class CrunchifyLogbackLogging { private final Logger crunchifyLogging = LoggerFactory . getLogger ( CrunchifyLogbackLogging . class ) ; public static void main ( String [ ] args ) { CrunchifyLogbackLogging crunchifyLogger = new CrunchifyLogbackLogging ( ) ; crunchifyLogger . getStartedwithLogBackTesting ( ) ; } private void getStartedwithLogBackTesting ( ) { for ( int i = 1 ; i < = 10 ; i ++ ) { crunchifyLogging . debug ( "......This is test by Crunchify on Logback DEBUG...... " ) ; crunchifyLogging . info ( "......This is test by Crunchify on Logback INFO......" ) ; crunchifyLogging . error ( "......This is test by Crunchify on Logback ERROR......" ) ; crunchifyLogging . warn ( "......This is test by Crunchify on Logback WARN......" ) ; } } } |
นี่เป็นคลาส Java Test ที่ง่ายมาก ซึ่งเราใช้ข้อผิดพลาด ข้อมูล ดีบัก และเตือนระดับการบันทึกทั้ง 4 ระดับ เราวนซ้ำ 10 รอบ
ขั้นตอนที่ 3
เมื่อคุณเรียกใช้โปรแกรมข้างต้น เฟรมเวิร์กการล็อกแบ็คจะสร้างไฟล์ใหม่สองไฟล์ดังที่กล่าวไว้ข้างต้น

ชำระเงิน Eclipse Console ของคุณ คุณจะเห็นบันทึกด้านล่าง:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
15 : 36 : 12 , 978 | - INFO in ch . qos . logback . classic . LoggerContext [ default ] - Could NOT find resource [ logback - test . xml ] 15 : 36 : 12 , 978 | - INFO in ch . qos . logback . classic . LoggerContext [ default ] - Could NOT find resource [ logback . groovy ] 15 : 36 : 12 , 978 | - INFO in ch . qos . logback . classic . LoggerContext [ default ] - Found resource [ logback . xml ] at [ file : / Users / ashah / Documents / site / Bitbucket / crunchifytutorials / target / classes / logback . xml ] 15 : 36 : 13 , 077 | - INFO in ch . qos . logback . classic . joran . action . ConfigurationAction - debug attribute not set 15 : 36 : 13 , 077 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - About to instantiate appender of type [ ch . qos . logback . core . ConsoleAppender ] 15 : 36 : 13 , 087 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - Naming appender as [ CRUNCHIFYOUT ] 15 : 36 : 13 , 143 | - WARN in ch . qos . logback . core . ConsoleAppender [ CRUNCHIFYOUT ] - This appender no longer admits a layout as a sub - component , set an encoder instead . 15 : 36 : 13 , 143 | - WARN in ch . qos . logback . core . ConsoleAppender [ CRUNCHIFYOUT ] - To ensure compatibility , wrapping your layout in LayoutWrappingEncoder . 15 : 36 : 13 , 143 | - WARN in ch . qos . logback . core . ConsoleAppender [ CRUNCHIFYOUT ] - See also http : //logback.qos.ch/codes.html#layoutInsteadOfEncoder for details 15 : 36 : 13 , 144 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - About to instantiate appender of type [ ch . qos . logback . core . FileAppender ] 15 : 36 : 13 , 147 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - Naming appender as [ CRUNCHIFYFILE ] 15 : 36 : 13 , 147 | - INFO in ch . qos . logback . core . joran . action . NestedComplexPropertyIA - Assuming default type [ ch . qos . logback . classic . encoder . PatternLayoutEncoder ] for [ encoder ] property 15 : 36 : 13 , 148 | - INFO in ch . qos . logback . core . FileAppender [ CRUNCHIFYFILE ] - File property is set to [ / Users / ashah / Documents / crunchify . log ] 15 : 36 : 13 , 151 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - About to instantiate appender of type [ ch . qos . logback . core . rolling . RollingFileAppender ] 15 : 36 : 13 , 152 | - INFO in ch . qos . logback . core . joran . action . AppenderAction - Naming appender as [ CRUNCHIFYROLLING ] 15 : 36 : 13 , 159 | - INFO in c . q . l . core . rolling . TimeBasedRollingPolicy @ 2096171631 - setting totalSizeCap to 1 GB 15 : 36 : 13 , 161 | - INFO in c . q . l . core . rolling . TimeBasedRollingPolicy @ 2096171631 - No compression will be used 15 : 36 : 13 , 163 | - INFO in c . q . l . core . rolling . TimeBasedRollingPolicy @ 2096171631 - Will use the pattern crunchify . log . timeroll . % d { yyyy - MM - dd } . log for the active file 15 : 36 : 13 , 166 | - INFO in c . q . l . core . rolling . DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'crunchify.log.timeroll.%d{yyyy-MM-dd}.log' . 15 : 36 : 13 , 166 | - INFO in c . q . l . core . rolling . DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll - over at midnight . 15 : 36 : 13 , 171 | - INFO in c . q . l . core . rolling . DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Sep 07 11 : 42 : 27 CDT 2017 15 : 36 : 13 , 173 | - INFO in ch . qos . logback . core . rolling . RollingFileAppender [ CRUNCHIFYROLLING ] - Active log file name : / Users / ashah / Documents / crunchify - rolling . log 15 : 36 : 13 , 173 | - INFO in ch . qos . logback . core . rolling . RollingFileAppender [ CRUNCHIFYROLLING ] - File property is set to [ / Users / ashah / Documents / crunchify - rolling . log ] 15 : 36 : 13 , 173 | - INFO in ch . qos . logback . classic . joran . action . RootLoggerAction - Setting level of ROOT logger to DEBUG 15 : 36 : 13 , 174 | - INFO in ch . qos . logback . core . joran . action . AppenderRefAction - Attaching appender named [ CRUNCHIFYOUT ] to Logger [ ROOT ] 15 : 36 : 13 , 174 | - INFO in ch . qos . logback . core . joran . action . AppenderRefAction - Attaching appender named [ CRUNCHIFYFILE ] to Logger [ ROOT ] 15 : 36 : 13 , 174 | - INFO in ch . qos . logback . core . joran . action . AppenderRefAction - Attaching appender named [ CRUNCHIFYROLLING ] to Logger [ ROOT ] 15 : 36 : 13 , 174 | - INFO in ch . qos . logback . classic . joran . action . ConfigurationAction - End of configuration . 15 : 36 : 13 , 175 | - INFO in ch . qos . logback . classic . joran . JoranConfigurator @ 7e0babb1 - Registering current configuration as safe fallback point 15 : 36 : 13.181 [ main ] DEBUG c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback DEBUG . . . . . . 15 : 36 : 13.186 [ main ] INFO c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback INFO . . . . . . 15 : 36 : 13.187 [ main ] ERROR c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback ERROR . . . . . . 15 : 36 : 13.187 [ main ] WARN c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback WARN . . . . . . 15 : 36 : 13.188 [ main ] DEBUG c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback DEBUG . . . . . . 15 : 36 : 13.188 [ main ] INFO c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback INFO . . . . . . 15 : 36 : 13.188 [ main ] ERROR c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback ERROR . . . . . . 15 : 36 : 13.188 [ main ] WARN c . c . tutorial . CrunchifyLogbackLogging - . . . . . . This is test by Crunchify on Logback WARN . . . . . . |
นอกจากนั้น คุณยังสามารถตรวจสอบบันทึกไฟล์ในเครื่องได้ และอย่างที่คุณเห็น ไฟล์ logback.xml
ได้สร้างไฟล์ใหม่ทั้งหมดและเริ่มพิมพ์บันทึกภายในไฟล์


ขอให้สนุก คุณได้ตั้งค่า logback.xml และส่วนต่อท้ายทั้ง 3 รายการอย่างถูกต้อง: ConsoleAppender, FileAppender, RollingFileAppender
คะแนนโบนัส:
คุณต้องการตั้งค่า RollingFileAppender
ด้วย FixedWindowRollingPolicy
หรือไม่? ใช้ appender ด้านล่างและใส่ลงในไฟล์ logback.xml และคุณน่าจะดีทั้งหมด
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
< appender name = "CRUNCHIFYROLLINGFIXEDWINDOW" class = "ch.qos.logback.core.rolling.RollingFileAppender" > < file > / Users / ashah / Documents / crunchify - rolling - fixwindows . log < / file > < encoder class = "ch.qos.logback.classic.encoder.PatternLayoutEncoder" > < pattern > % date % level [ % thread ] % logger { 10 } [ % file : % line ] % msg % n < / pattern > < / encoder > < rollingPolicy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" > < FileNamePattern > crunchify . log . fixedsize . { yyyy - MM - dd } % i . log < / FileNamePattern > < MinIndex > 1 < / MinIndex > < MaxIndex > 15 < / MaxIndex > < / rollingPolicy > < triggeringPolicy class = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" > < MaxFileSize > 20MB < / MaxFileSize > < / triggeringPolicy > < / appender > |
นอกจากนี้ เพียงเพิ่ม appender-ref ภายใต้องค์ประกอบรูท
1 |
< appender - ref ref = "CRUNCHIFYROLLINGFIXEDWINDOW" / > |