SlideShare a Scribd company logo
Slf4j+Logback Presentation
Contents
 1.Logging

 2.Introduction to slf4j logging API

 3.Logback vs. Log4j

 4.About Logback

 5.Logback Architecture

 6.Components of Logback

 7.Configuration of Logback

 8.Basic logback.xml

 9.Convenience with logback

 10.Conventions with logback

 11.Links
  Logging as we all know is a way of recording events at an appropriate output
   store.
  Based on convenience we can get the output at the console, file, database or
   email.
  The process is capable of stating the flow of execution and can even be helpful
   to locate
the error points in an application.
Introduction to slf4j logging API.
 •   SLF4J is only a facade, meaning that it does not provide a complete logging solution.
 •   With any other logging solution working at the back end, slf4j acts a consistent logging
     system and keeps the user unknown about which logging mechanism is being used.




 •   slf4j.zip ships with bindings for all logging mechanisms. User can switch to any logging
     just by changing to the corresponding jar file.
Logback vs Log4j
                    Log4j                                        Logback
 Can be directly implemented in                Logback natively speaks slf4j i.e, it exposes
 applications.                                 it’s API via slf4j.


 Log4j can be configured using xml file as     Logback can be configured in a xml file and
 well as properties file.                      groovy. There are online application to
                                               convert an existing log4j.properties to a
                                               logback.xml file.


 Once log4j is implemented at the code level   Logback project can be shifted to any other
 Its becomes difficult to move to any other    logging mechanism easily only by removing
 logging mechanism.                            logback.jar and placing the other required
                                               slf4j binding.
About Logback
 •   Logback is intended as a successor to the popular log4j project.
 •   It is no revolution but evolution, as the author of logback Sébastien Pennec says.
 •   It edges log4j as log4j is merely developer’s logging system to trace the execution
     of a program whereas logback is even capable of generating server side logs when
     a use accesses a webpage on the server.
 •   Logback exposes it’s API via slf4j and can be easily replaced by any other logging
     mechanism depending upon the use case scenario.
 •   In some situations it has even been upto 10 times efficient than log4j.
Architecture of logback
                              Logback in its current release is divided
                              into three modules.
                          •   Logback-core : It lays the ground work
                              for the other two modules. Acts as the
                              super class to some of the classes in the
                              other modules.
                          •   Logback-classic : Logback classic
                              extends the core module and internally
                              implements slf4j so that the user can
                              readily switch back and fourth between
                              various other logging mechanisms.
                          •   Logback -access : Logback –access
                              integrates with Servlet containers to
                              provide HTTP-access log functionality.
                          •   Slf4j acts as the interface to various
                              other logging mechanisms.
Components of Logback.
 The main components of logback are as follows.
 •   Appender
 •   Encoder
 •   Layout
 •   Filter
 •   Logging Levels
•   Logback appender is a part of logback-core module.
•   Logback delegates the task of writing a logging event to components called appenders
•   Different appenders override doAppend(Event e) method and write the events to the
    specified appender.
Layout: Layout are capable to accept events and write those events as a strings.
  Until Logback 0.9.19 appenders directly relied on layouts to write the event at
   the appropriate output destination.
  Layouts had no control on when the event has to be written out so could not
   aggregate events into batches.
  Accepts the format in which the output has to be written out.
Encoder: They accept events and transform these events directly into byte array
   and
       write into the output stream.
  Binds a layout into it that specifies the format of the output.
  Unlike layout has total control over when the data has to be written out.

Filter: These are the logback-classic components that are capable of filtering the
    incoming events based on the specified rule and can deny or accept the
    event for being            printed.
ex. <level>debug</level>
   <onMatch>Accpet</onMatch>
   <onMismatch>Deny</onMismatch>
Logback offers six levels of
Logging. They are as mentioned                             Effectiveness

below:-
  Trace                       Requested   Trace   Debug   Info   Warn     Error   Off
                               level
  Debug
                               Trace       yes     no      no     no       no      no
  Info
  Warn
  Error                       Debug       yes     yes     no     no       no      no
  Off
The table shows each level and
                               Info        yes     yes     yes    no       no      no
their effectiveness.

                               Warn        yes     yes     yes    yes      no      no



                               Error       yes     yes     yes    yes      yes     no
   Logback can be configured into a .xml file or .groovy file.
   During the initialization of logback in an application it tries to load the
    logback.groovy file.
   In the absence of logback.groovy file, logback looks for logback-test.xml file and
    if not available it search for logback.xml file to configure itself.
   If the above mentioned file is not present in the application,logback configures
    itself using BasicConfigurator which will cause all the logging statements to be
    directed to the console.

Lets see how to configure all these Logback components in an xml file.
Given below are some sample logback.xml files highlighting the use of
   appender,encoder,pattern,Logging levels and logger.
Logback
Logback
   Logback-classic always checks for any modification in the configuration and is
    capable of applying the changes if found dynamically.
ex - <configuration scan="true" scanPeriod="10 seconds" debug="true">
    Logback allows parameterized logging statement.
ex – This approach eliminates the overhead of creating extra string in the below mentioned
     statement.
String name = “Anubhav”;
if(debugEnabled)
{logger.debug(“the name to be printed is”+name);}
Instead it can be
String name = “Anubhav”;
Logger.debug(“The name to printed is {}”,name);

   A developer can even trace the internal status of the logback processing by
    calling upon the LoggerContext and printing its status as follows.
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
StatusPrinter.print(lc);
   Directory structures can be mentioned as properties tags in the xml file to avoid
    retyping
everytime.
ex -<properties name =”Logback_Dir” value=”d:/logFiles”>
Can be referenced as
<file>${Logback_Dir}/logFile.log</file>
   Resource files can also be configured to be loaded and the keys can be
    referenced from
logback.xml file.
ex- <property file=”src/MessageResource.properties.”>.
The key can be referenced as
<mail_to>${MailTo}</mail_to>
    Let the declaration of logger be always the first statement inside a class.
    Use trace level only when tracing the whole application is necessary as it
     activates
all levels.
    While entering a function the logger statement should look like
logger.debug(“>>Entering the function”);
    While exiting the function the logger statement should look like
logger.debug(“<<Exiting the function”);
    The instantiation of an object should be captured as
logger.debug(“** new ClassConstructor()”);
    When inside a method the log statement should always contain the method
     name
logger.debug(“myFunction()>the parameter is {}”,parameter);

    Use appropriate tool to shift log4j configured project to slf4j.
ex- slt4j migrator. it will replace appropriate import lines and logger declarations.

    Place slf4j.jar,logback-core.jar and logback-classic.jar in the classpath.

    Use the following tool to convert log4j.properties to logback.xml and deploy the
    xml file.
Link - http://logback.qos.ch/translator/
                                      Shift Complete
   http://logback.qos.ch/
   http://logback.qos.ch/documentation.html
   http://logback.qos.ch/manual/index.html
   http://logback.qos.ch/manual/appenders.html
   http://logback.qos.ch/manual/encoders.html
   http://logback.qos.ch/manual/layouts.html
   http://logback.qos.ch/manual/filters.html
   https://wiki.base22.com/display/btg/Java+Logging+Standards+and+Guideli
    nes
Thank You
Ad

More Related Content

What's hot (20)

Intro to Git and GitHub
Intro to Git and GitHubIntro to Git and GitHub
Intro to Git and GitHub
Panagiotis Papadopoulos
 
Tomcat
TomcatTomcat
Tomcat
Venkat Pinagadi
 
From Spring Framework 5.3 to 6.0
From Spring Framework 5.3 to 6.0From Spring Framework 5.3 to 6.0
From Spring Framework 5.3 to 6.0
VMware Tanzu
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Tomcat and apache httpd training
Tomcat and apache httpd trainingTomcat and apache httpd training
Tomcat and apache httpd training
Franck SIMON
 
From Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdfFrom Java 11 to 17 and beyond.pdf
From Java 11 to 17 and beyond.pdf
José Paumard
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
NexThoughts Technologies
 
Logging
LoggingLogging
Logging
Марія Русин
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
Trey Howard
 
Grafana
GrafanaGrafana
Grafana
NoelMc Grath
 
Introduction to GitHub Actions
Introduction to GitHub ActionsIntroduction to GitHub Actions
Introduction to GitHub Actions
Knoldus Inc.
 
Yaml
YamlYaml
Yaml
Sabarinath Gnanasekar
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 
카프카, 산전수전 노하우
카프카, 산전수전 노하우카프카, 산전수전 노하우
카프카, 산전수전 노하우
if kakao
 
Spring boot
Spring bootSpring boot
Spring boot
Gyanendra Yadav
 
Prometheus Overview
Prometheus OverviewPrometheus Overview
Prometheus Overview
Brian Brazil
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
Guo Albert
 

Viewers also liked (20)

LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
Logging library migrations - A case study for the Apache Software Foundation ...
Logging library migrations - A case study for the Apache Software Foundation ...Logging library migrations - A case study for the Apache Software Foundation ...
Logging library migrations - A case study for the Apache Software Foundation ...
corpaulbezemer
 
School objects teacher Helena (Clase de los Marcianitos)
School objects teacher Helena (Clase de los Marcianitos)School objects teacher Helena (Clase de los Marcianitos)
School objects teacher Helena (Clase de los Marcianitos)
María Dolores López Yustres
 
Drogas no gracias
Drogas no graciasDrogas no gracias
Drogas no gracias
Benjamín González
 
Micro 10
Micro 10Micro 10
Micro 10
Egarsat SP
 
Seminario Concon
Seminario ConconSeminario Concon
Seminario Concon
guestef05b7
 
Polish Saturday School employment
Polish Saturday School employment Polish Saturday School employment
Polish Saturday School employment
Karolina Forbes
 
Paridera del parque
Paridera del parqueParidera del parque
Paridera del parque
iesmonreal
 
French Digital Republic Act
French Digital Republic ActFrench Digital Republic Act
French Digital Republic Act
Jan Dhont
 
#Xm@s Marketing Checklist (website, e-mail, promotions)
#Xm@s Marketing Checklist (website, e-mail, promotions)#Xm@s Marketing Checklist (website, e-mail, promotions)
#Xm@s Marketing Checklist (website, e-mail, promotions)
Ewa Sámek
 
7 Tools for your Puppetized Devops stack
7 Tools for your Puppetized Devops stack7 Tools for your Puppetized Devops stack
7 Tools for your Puppetized Devops stack
Kris Buytaert
 
TEGNOLOGIA INNOVACION
TEGNOLOGIA INNOVACIONTEGNOLOGIA INNOVACION
TEGNOLOGIA INNOVACION
diego diaz
 
Ericsson ConsumerLab: Smartphone Usage Experience Report
Ericsson ConsumerLab: Smartphone Usage Experience ReportEricsson ConsumerLab: Smartphone Usage Experience Report
Ericsson ConsumerLab: Smartphone Usage Experience Report
Ericsson
 
Zoom Interiors by Egger & DixiePly
Zoom Interiors by Egger & DixiePlyZoom Interiors by Egger & DixiePly
Zoom Interiors by Egger & DixiePly
Rafael A. Caprile
 
Memorias Avanzado[1]
Memorias Avanzado[1]Memorias Avanzado[1]
Memorias Avanzado[1]
guesta4a90
 
Telered.com.es
Telered.com.esTelered.com.es
Telered.com.es
puchi286
 
Firefox OS - Answering global challenges
Firefox OS - Answering global challengesFirefox OS - Answering global challenges
Firefox OS - Answering global challenges
Christian Heilmann
 
Cultura
CulturaCultura
Cultura
Marisa Pérez Teijeiro
 
21 Spieltag Ausfall Hockenheim
21  Spieltag   Ausfall Hockenheim21  Spieltag   Ausfall Hockenheim
21 Spieltag Ausfall Hockenheim
guest02f2f9af
 
Arquitectos & Ingenieros Ejemplo
Arquitectos & Ingenieros EjemploArquitectos & Ingenieros Ejemplo
Arquitectos & Ingenieros Ejemplo
Eduardo Hdz
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
Logging library migrations - A case study for the Apache Software Foundation ...
Logging library migrations - A case study for the Apache Software Foundation ...Logging library migrations - A case study for the Apache Software Foundation ...
Logging library migrations - A case study for the Apache Software Foundation ...
corpaulbezemer
 
School objects teacher Helena (Clase de los Marcianitos)
School objects teacher Helena (Clase de los Marcianitos)School objects teacher Helena (Clase de los Marcianitos)
School objects teacher Helena (Clase de los Marcianitos)
María Dolores López Yustres
 
Seminario Concon
Seminario ConconSeminario Concon
Seminario Concon
guestef05b7
 
Polish Saturday School employment
Polish Saturday School employment Polish Saturday School employment
Polish Saturday School employment
Karolina Forbes
 
Paridera del parque
Paridera del parqueParidera del parque
Paridera del parque
iesmonreal
 
French Digital Republic Act
French Digital Republic ActFrench Digital Republic Act
French Digital Republic Act
Jan Dhont
 
#Xm@s Marketing Checklist (website, e-mail, promotions)
#Xm@s Marketing Checklist (website, e-mail, promotions)#Xm@s Marketing Checklist (website, e-mail, promotions)
#Xm@s Marketing Checklist (website, e-mail, promotions)
Ewa Sámek
 
7 Tools for your Puppetized Devops stack
7 Tools for your Puppetized Devops stack7 Tools for your Puppetized Devops stack
7 Tools for your Puppetized Devops stack
Kris Buytaert
 
TEGNOLOGIA INNOVACION
TEGNOLOGIA INNOVACIONTEGNOLOGIA INNOVACION
TEGNOLOGIA INNOVACION
diego diaz
 
Ericsson ConsumerLab: Smartphone Usage Experience Report
Ericsson ConsumerLab: Smartphone Usage Experience ReportEricsson ConsumerLab: Smartphone Usage Experience Report
Ericsson ConsumerLab: Smartphone Usage Experience Report
Ericsson
 
Zoom Interiors by Egger & DixiePly
Zoom Interiors by Egger & DixiePlyZoom Interiors by Egger & DixiePly
Zoom Interiors by Egger & DixiePly
Rafael A. Caprile
 
Memorias Avanzado[1]
Memorias Avanzado[1]Memorias Avanzado[1]
Memorias Avanzado[1]
guesta4a90
 
Telered.com.es
Telered.com.esTelered.com.es
Telered.com.es
puchi286
 
Firefox OS - Answering global challenges
Firefox OS - Answering global challengesFirefox OS - Answering global challenges
Firefox OS - Answering global challenges
Christian Heilmann
 
21 Spieltag Ausfall Hockenheim
21  Spieltag   Ausfall Hockenheim21  Spieltag   Ausfall Hockenheim
21 Spieltag Ausfall Hockenheim
guest02f2f9af
 
Arquitectos & Ingenieros Ejemplo
Arquitectos & Ingenieros EjemploArquitectos & Ingenieros Ejemplo
Arquitectos & Ingenieros Ejemplo
Eduardo Hdz
 
Ad

Similar to Logback (20)

Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
Knoldus Inc.
 
Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
Knoldus Inc.
 
Rein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4jRein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4j
Razorsight
 
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Chirag Thumar
 
Log4e
Log4eLog4e
Log4e
husnara mohammad
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
Rajiv Gupta
 
SLF4J+Logback
SLF4J+LogbackSLF4J+Logback
SLF4J+Logback
Guo Albert
 
Log4j
Log4jLog4j
Log4j
Krishnakanth Goud
 
Logging Services for .net - log4net
Logging Services for .net - log4netLogging Services for .net - log4net
Logging Services for .net - log4net
Guo Albert
 
Milano Meetups #15 - Log Forwarding.pptx
Milano Meetups #15 - Log Forwarding.pptxMilano Meetups #15 - Log Forwarding.pptx
Milano Meetups #15 - Log Forwarding.pptx
Gonzalo Marcos Ansoain
 
Milano Meetups #15 - Log Forwarding.pptx.pdf
Milano Meetups #15 - Log Forwarding.pptx.pdfMilano Meetups #15 - Log Forwarding.pptx.pdf
Milano Meetups #15 - Log Forwarding.pptx.pdf
Florence Consulting
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
Logging.pptxbjjjjbhhn bhnjnnnnnnnnnnnn
Logging.pptxbjjjjbhhn   bhnjnnnnnnnnnnnnLogging.pptxbjjjjbhhn   bhnjnnnnnnnnnnnn
Logging.pptxbjjjjbhhn bhnjnnnnnnnnnnnn
anshumankapooriitbco
 
Logging best practice in mule using logger component
Logging best practice in mule using logger componentLogging best practice in mule using logger component
Logging best practice in mule using logger component
Govind Mulinti
 
Log4e
Log4eLog4e
Log4e
Gagandeep Singh
 
Log4j is a reliable, fast and flexible
Log4j is a reliable, fast and flexibleLog4j is a reliable, fast and flexible
Log4j is a reliable, fast and flexible
Ramakrishna kapa
 
Logging and Exception
Logging and ExceptionLogging and Exception
Logging and Exception
Azeem Mumtaz
 
SLF4J Explained........
SLF4J Explained........SLF4J Explained........
SLF4J Explained........
Sunitha Satyadas
 
Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
Knoldus Inc.
 
Logging with Logback in Scala
Logging with Logback in ScalaLogging with Logback in Scala
Logging with Logback in Scala
Knoldus Inc.
 
Rein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4jRein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4j
Razorsight
 
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Log4j with selenium tutorial: How to Setup log4j logging in selenium automati...
Chirag Thumar
 
Java Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4jJava Logging discussion Log4j,Slf4j
Java Logging discussion Log4j,Slf4j
Rajiv Gupta
 
Logging Services for .net - log4net
Logging Services for .net - log4netLogging Services for .net - log4net
Logging Services for .net - log4net
Guo Albert
 
Milano Meetups #15 - Log Forwarding.pptx
Milano Meetups #15 - Log Forwarding.pptxMilano Meetups #15 - Log Forwarding.pptx
Milano Meetups #15 - Log Forwarding.pptx
Gonzalo Marcos Ansoain
 
Milano Meetups #15 - Log Forwarding.pptx.pdf
Milano Meetups #15 - Log Forwarding.pptx.pdfMilano Meetups #15 - Log Forwarding.pptx.pdf
Milano Meetups #15 - Log Forwarding.pptx.pdf
Florence Consulting
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
LOGBack and SLF4J
LOGBack and SLF4JLOGBack and SLF4J
LOGBack and SLF4J
jkumaranc
 
Logging.pptxbjjjjbhhn bhnjnnnnnnnnnnnn
Logging.pptxbjjjjbhhn   bhnjnnnnnnnnnnnnLogging.pptxbjjjjbhhn   bhnjnnnnnnnnnnnn
Logging.pptxbjjjjbhhn bhnjnnnnnnnnnnnn
anshumankapooriitbco
 
Logging best practice in mule using logger component
Logging best practice in mule using logger componentLogging best practice in mule using logger component
Logging best practice in mule using logger component
Govind Mulinti
 
Log4j is a reliable, fast and flexible
Log4j is a reliable, fast and flexibleLog4j is a reliable, fast and flexible
Log4j is a reliable, fast and flexible
Ramakrishna kapa
 
Logging and Exception
Logging and ExceptionLogging and Exception
Logging and Exception
Azeem Mumtaz
 
Ad

Logback

  • 2. Contents 1.Logging 2.Introduction to slf4j logging API 3.Logback vs. Log4j 4.About Logback 5.Logback Architecture 6.Components of Logback 7.Configuration of Logback 8.Basic logback.xml 9.Convenience with logback 10.Conventions with logback 11.Links
  • 3.  Logging as we all know is a way of recording events at an appropriate output store.  Based on convenience we can get the output at the console, file, database or email.  The process is capable of stating the flow of execution and can even be helpful to locate the error points in an application.
  • 4. Introduction to slf4j logging API. • SLF4J is only a facade, meaning that it does not provide a complete logging solution. • With any other logging solution working at the back end, slf4j acts a consistent logging system and keeps the user unknown about which logging mechanism is being used. • slf4j.zip ships with bindings for all logging mechanisms. User can switch to any logging just by changing to the corresponding jar file.
  • 5. Logback vs Log4j Log4j Logback Can be directly implemented in Logback natively speaks slf4j i.e, it exposes applications. it’s API via slf4j. Log4j can be configured using xml file as Logback can be configured in a xml file and well as properties file. groovy. There are online application to convert an existing log4j.properties to a logback.xml file. Once log4j is implemented at the code level Logback project can be shifted to any other Its becomes difficult to move to any other logging mechanism easily only by removing logging mechanism. logback.jar and placing the other required slf4j binding.
  • 6. About Logback • Logback is intended as a successor to the popular log4j project. • It is no revolution but evolution, as the author of logback Sébastien Pennec says. • It edges log4j as log4j is merely developer’s logging system to trace the execution of a program whereas logback is even capable of generating server side logs when a use accesses a webpage on the server. • Logback exposes it’s API via slf4j and can be easily replaced by any other logging mechanism depending upon the use case scenario. • In some situations it has even been upto 10 times efficient than log4j.
  • 7. Architecture of logback Logback in its current release is divided into three modules. • Logback-core : It lays the ground work for the other two modules. Acts as the super class to some of the classes in the other modules. • Logback-classic : Logback classic extends the core module and internally implements slf4j so that the user can readily switch back and fourth between various other logging mechanisms. • Logback -access : Logback –access integrates with Servlet containers to provide HTTP-access log functionality. • Slf4j acts as the interface to various other logging mechanisms.
  • 8. Components of Logback. The main components of logback are as follows. • Appender • Encoder • Layout • Filter • Logging Levels
  • 9. Logback appender is a part of logback-core module. • Logback delegates the task of writing a logging event to components called appenders • Different appenders override doAppend(Event e) method and write the events to the specified appender.
  • 10. Layout: Layout are capable to accept events and write those events as a strings.  Until Logback 0.9.19 appenders directly relied on layouts to write the event at the appropriate output destination.  Layouts had no control on when the event has to be written out so could not aggregate events into batches.  Accepts the format in which the output has to be written out. Encoder: They accept events and transform these events directly into byte array and write into the output stream.  Binds a layout into it that specifies the format of the output.  Unlike layout has total control over when the data has to be written out. Filter: These are the logback-classic components that are capable of filtering the incoming events based on the specified rule and can deny or accept the event for being printed. ex. <level>debug</level> <onMatch>Accpet</onMatch> <onMismatch>Deny</onMismatch>
  • 11. Logback offers six levels of Logging. They are as mentioned Effectiveness below:-  Trace Requested Trace Debug Info Warn Error Off level  Debug Trace yes no no no no no  Info  Warn  Error Debug yes yes no no no no  Off The table shows each level and Info yes yes yes no no no their effectiveness. Warn yes yes yes yes no no Error yes yes yes yes yes no
  • 12. Logback can be configured into a .xml file or .groovy file.  During the initialization of logback in an application it tries to load the logback.groovy file.  In the absence of logback.groovy file, logback looks for logback-test.xml file and if not available it search for logback.xml file to configure itself.  If the above mentioned file is not present in the application,logback configures itself using BasicConfigurator which will cause all the logging statements to be directed to the console. Lets see how to configure all these Logback components in an xml file.
  • 13. Given below are some sample logback.xml files highlighting the use of appender,encoder,pattern,Logging levels and logger.
  • 16. Logback-classic always checks for any modification in the configuration and is capable of applying the changes if found dynamically. ex - <configuration scan="true" scanPeriod="10 seconds" debug="true">  Logback allows parameterized logging statement. ex – This approach eliminates the overhead of creating extra string in the below mentioned statement. String name = “Anubhav”; if(debugEnabled) {logger.debug(“the name to be printed is”+name);} Instead it can be String name = “Anubhav”; Logger.debug(“The name to printed is {}”,name);  A developer can even trace the internal status of the logback processing by calling upon the LoggerContext and printing its status as follows. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); StatusPrinter.print(lc);
  • 17. Directory structures can be mentioned as properties tags in the xml file to avoid retyping everytime. ex -<properties name =”Logback_Dir” value=”d:/logFiles”> Can be referenced as <file>${Logback_Dir}/logFile.log</file>  Resource files can also be configured to be loaded and the keys can be referenced from logback.xml file. ex- <property file=”src/MessageResource.properties.”>. The key can be referenced as <mail_to>${MailTo}</mail_to>
  • 18. Let the declaration of logger be always the first statement inside a class.  Use trace level only when tracing the whole application is necessary as it activates all levels.  While entering a function the logger statement should look like logger.debug(“>>Entering the function”);  While exiting the function the logger statement should look like logger.debug(“<<Exiting the function”);  The instantiation of an object should be captured as logger.debug(“** new ClassConstructor()”);  When inside a method the log statement should always contain the method name logger.debug(“myFunction()>the parameter is {}”,parameter);
  • 19. Use appropriate tool to shift log4j configured project to slf4j. ex- slt4j migrator. it will replace appropriate import lines and logger declarations.  Place slf4j.jar,logback-core.jar and logback-classic.jar in the classpath.  Use the following tool to convert log4j.properties to logback.xml and deploy the xml file. Link - http://logback.qos.ch/translator/ Shift Complete
  • 20. http://logback.qos.ch/  http://logback.qos.ch/documentation.html  http://logback.qos.ch/manual/index.html  http://logback.qos.ch/manual/appenders.html  http://logback.qos.ch/manual/encoders.html  http://logback.qos.ch/manual/layouts.html  http://logback.qos.ch/manual/filters.html  https://wiki.base22.com/display/btg/Java+Logging+Standards+and+Guideli nes