The presentation demonstrates how to implement logback in Java using SLF4J.
Size: 1.01 MB
Language: en
Added: May 29, 2024
Slides: 3 pages
Slide Content
•Appender name: RollingFile
•When the size of the log file is 10MB,
it will be moved to an archive folder
and a new log file will be created.
•class = RollingFileAppender
RollingFile Appender
Insert this XML file in the “src/main/resources” folder:
logback-spring.xml
By: Scott Michael Anderson
Date:07/ 13/2021
< ?xml v ersion="l. 8" encoding="UTF-8"?>
<I·· NOTE: this file MUST be named logback-spring.xml -->
<configuration>
<property name="lOGS" value="./logs" />
<appender name="Console"
class= "ch. qos. logback. core. ConsoleAppender ">
<layout class="ch. qos. logback. classic. Patternlayout">
<Pattern>
Xblack(Xd{IS08601}) Xhighlight(X· 5level) [Xblue(%t) J Xyellow(XC{l.}): XmsgXnXthrowable
</Pattern>
</layout>
</appender>
_ .. .,_
"
class= "ch. qos. logback. core. rolling. RoLL ingFiLeAppender">
<file>${ LOGS}/ spring-boot-logger. log</file>
<encoder
class="ch.qos. logback. classic .encoder. PatternlayoutEncoder">
<Pattern>Xd Xp XC{ l.} [Xt) XmXn</Pattern>
</encoder>
<rollingPolicy
class= "ch. qos. Logback. core. roLL ing. TimeBasedRoLL ingPoL i cy">
<I--rollover daily and when the file reaches 10 MegaBy tes -->
<fi leNamePat tern>${ LOGS}/ archived/spring-boot-logger-Xd{yyyy- t-V-1-dd}. Xi . log
<If ileNamePatte rn >
<timeBasedFileNamingAndTriggeringPolicy
class= "ch. qos. Logback. core. rol Ling. SizeAndTimeBasedFNATP">
<max F ileSize > 10MB< /maxf i leSize >
<
/timeBa s edF ileNamingAndTriggeringPol icy>
</rollingPolicy>
</appender>
<appender name="Roll ingFileApp" ____ ,_
.f/.f.lV::O. ---~_, .... ,.. ~...-, ........... ~ ... , ........ ~. --~_,-.. ---
<file>${ LOGS}/ spring-boot-logger-app. log</file>
<encoder
class= "ch. qos. Logback. classic. encoder. PatternlayoutEncoder ">
<Pattern>Xd Xp XC{l.} [Xt] XmXn</Pattern>
I I
</encoder>
~
~ <rollingPolicy
class="ch.qos. Logback. core. roLL ing. TimeBasedRoLL ingPol icy">
<l--rollover daily and when the file reaches 10 MegaBy tes -->
<fileNamePattern>S{LOGS}/archived/spring-boot-logger-app-Xd{yyyy- f-V-1-dd}. Xi. log
< / f ileNameP atte rn >
Loq Level TRACE DEBUG INFO WARN ERROR
<timeBasedFileNamingAndTriggeringPolicy
"trace" X X X X X
class= "ch. qos. Logback. core. rol Ling. SizeAndTimeBasedFNATP">
<maxF i le Size> 10MB< /maxF i leSize >
"debug" X X X X
< I time Ba sedF i leNami ngAndTriggeringPol icy>
</rollingPolicy>
"info X X X
</appender>
"warn" X X
< 1--Logging at the Spring Boot level -->
"error" X -
<root level="info">
<appender-ref ref="Roll ingfile" />
"off"
<appender-ref ref-"Console" />
- - - - -
<tr"-
.
<!--Logging at the Film ~'I!;(;; FF r> -->
<logger name="edu. cpcc. Labs.dogo" 1eve1::debug" dditivity="false">
<appender-ref ref="RollingFiL ry;
</logger>
<I conf i2uration>
•AppenderName:ConsoleAppender
•The logs are written to theconsole
•class=ConsoleAppender
Console Appender
Insert this XML file in the “src/main/resources” folder:
logback-spring.xml
By: Scott Michael Anderson
Date:07/ 13/2021
< ?xml v ersion="l. 8" encoding="UTF-8"?>
<I·· NOTE: this file MUST be named logback-spring.xml -->
<configuration>
<property name="lOGS" value="./logs" />
<appender name="Console"
class= "ch. qos. logback. core. ConsoleAppender ">
<layout class="ch. qos. logback. classic. Patternlayout">
<Pattern>
Xblack(Xd{IS08601}) Xhighlight(X· 5level) [Xblue(%t) J Xyellow(XC{l.}): XmsgXnXthrowable
</Pattern>
</layout>
</appender>
<appender name="Roll ingFile"
class= "ch. qos. logback. core. rolling. RoLL ingFiLeAppender">
<file>${ LOGS}/ spring-boot-logger. log</file>
<encoder
class="ch.qos. logback. classic .encoder. PatternlayoutEncoder">
<Pattern>Xd Xp XC{ l.} [Xt) XmXn</Pattern>
</encoder>
<rollingPolicy
class= "ch. qos. Logback. core. roLL ing. TimeBasedRoLL ingPoL i cy">
<I--rollover daily and when the file reaches 10 MegaBy tes -->
<fi leNamePat tern>${ LOGS}/ archived/spring-boot-logger-Xd{yyyy- t-V-1-dd}. Xi . log
< I fileNamePatte rn >
<timeBasedFileNamingAndTriggeringPolicy
class= "ch. qos. Logback. core. rol Ling. SizeAndTimeBasedFNATP">
<max F ileSize > 10MB< /maxf i leSize >
<
/timeBa s edF ileNamingAndTriggeringPol icy>
</rollingPolicy>
</appender>
<appender name="Roll ingFileApp"
class= "ch. qos. Logback. core. rolling. Roll ingFileAppender">
<file>${ LOGS}/ spring-boot-logger-app. log</file>
<encoder
class= "ch. qos. Logback. classic. enc oder. PatternLayoutEncoder ">
<Pattern>Xd Xp XC{l.} [Xt] XmXn</Pattern>
I I
</encoder>
~
~ <rollingPolicy
class="ch.qos. Logback. core. roLL ing. TimeBasedRoLL ingPol icy">
<l--rollover daily and when the file reaches 10 MegaBy tes -->
<fileNamePattern>S{LOGS}/archived/spring-boot-logger-app-Xd{yyyy- f-V-1-dd}. Xi. log
< / f ileNameP atte rn >
Log Level ifRACE DEBUG INFO WARN ERROR
<timeBasedFileNamingAndTriggeringPolicy
class= "ch. qos. Logback. core. rol Ling. SizeAndTimeBasedFNATP">
"trace" X X X X X
<maxF i le Size> 10MB< /maxF i leSize >
"debuq" X X X X
< I time Sa sedF i leNami ngAndTriggeringPol icy>
</rollingPolicy>
"info X X X
</appender>
"warn" X X
< 1--Logging at the Spring Boot level -->
-
<root level="info">
"error" X
<appender-ref ref="Roll ingFile" />
"off"
<appender-ref ref-"Console" />
- - - - - <tr-
.
<!--Logging at the Film ~'I!;(;; FF r> -->
<logger name="edu. cpcc. Labs. dogo" 1eve1::debug" dditivity="false">
<appender-ref ref="RoLLingFiL ry;
</logger>
<I conf i2uration>
//Import the following
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//Instance of Logger class is created. Instance is named “logger”
// Initialize a logger for the class
Logger logger= LoggerFactory.getLogger({Name of the class}.class.getName());
//Insert code inside the method you want to log
logger.{Log Level}("Entered: getAllMEssages: ");
Logback Framework Code
By: Scott Michael Anderson
Date:07/ 13/2021
Log Level TRACE DEBUG INFO WARN ERROR
"trace" X X X X X
"debug" X X X X
''info X X X
"warn" X X
"error" X
"off" - - - - -