Ce este Logback.xml Logging Framework? Exemplu ConsoleAppender, FileAppender și RollingFileAppender
Publicat: 2019-02-13
Sunteți la curent cu Cadrul de Logback Logging Framework
? Ei bine, majoritatea oamenilor folosesc în principal log4j and slf4j
în proiectul lor. În ultimele 6 luni, folosesc cadrul de înregistrare Logback.xml
și aș spune că am văzut destul de multe îmbunătățiri de performanță în aplicația mea de producție.
În acest tutorial vom trece peste un exemplu practic de cadru de logare în logare.
Ce este logback.qos.ch?
Logback este un fel de o nouă versiune a Log4j. A fost dezvoltat de unde s-a oprit dezvoltarea log4j.

Pe Crunchify, am publicat mai multe tutoriale pe Log4j dacă sunteți interesat:
- Cum să vă configurați propriul nivel de înregistrare log4j?
- Cum se configurează log4j.properties?
Aici vom trece peste toți pașii detaliați despre cum să configurați cadru de logback pentru proiectul Enterprise Java. Vom crea mai jos fișiere noi.
- Sub folderul de resurse fișierul
logback.xml
- Clasa Java
CrunchifyLogbackLogging.java
Să începem
Pasul 1 Creați fișierul 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 > |
Să înțelegem mai întâi fișierul logback.xml.
- În partea de jos a unui fișier vedeți că avem 3 referințe
appender-ref
sub elementulroot
. - Folosim nivelul de jurnal ca
DEBUG
. Asta înseamnă să tipăriți totul cu nivelul DEBUG și mai sus.
CRUNCHIFYOUT
este din clasa ch.qos.logback.core. ConsoleAppender
. Ceea ce înseamnă că toate datele de înregistrare vor fi tipărite pe Consola Eclipse.
CRUNCHIFYFILE
este de tip ch.qos.logback.core. FileAppender
. Aceasta înseamnă că toate datele de înregistrare vor fi tipărite și în fișierul local. În cazul nostru este /Users/ ashah /Documents/ crunchify.log
.
CRUNCHIFYROLLING
este de tip ch.qos.logback.core.rolling. RollingFileAppender
. După cum vedeți în rollingPolicy, rulăm fișierele în fiecare zi și păstrăm maximum 10 fișiere. De asemenea, există o limită de capacitate pentru dimensiunea totală a fișierelor, care este de 1 GB
în exemplul nostru. Un fișier nou va fi creat în locația /Users/ ashah /Documents/ crunchify -rolling.log
.
Pasul 2 Scrieți programul Java pentru a-l testa.
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......" ) ; } } } |
Aceasta este o clasă de testare Java foarte simplă, în care folosim erori, informații, depanare și avertizare pe toate cele 4 niveluri de înregistrare. Facem buclă de 10 ori.
Pasul 3
Odată ce rulați programul de mai sus, cadrul de logback va crea două fișiere noi, așa cum s-a menționat mai sus.

Verificați Consola Eclipse. Veți vedea mai jos jurnalele:
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 . . . . . . |
În plus, puteți verifica și jurnalele de fișiere locale și, după cum vedeți, fișierul logback.xml
a creat toate fișierele noi și a început să imprime jurnale în interiorul acestuia.


Bucură-te, ai configurat corect logback.xml și toate cele 3 apendice: ConsoleAppender, FileAppender, RollingFileAppender.
Punct bonus:
Doriți să configurați RollingFileAppender
cu FixedWindowRollingPolicy
? Ei bine, utilizați apendicele de mai jos și puneți-l în fișierul logback.xml și ar trebui să fiți bine
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 > |
De asemenea, trebuie doar să adăugați appender-ref sub elementul rădăcină.
1 |
< appender - ref ref = "CRUNCHIFYROLLINGFIXEDWINDOW" / > |