KEMBAR78
Logging with Logback in Scala | ODP
Logging with Logback
in Scala
Anurag Srivastava
Software Consultant
Knoldus Software LLP
Agenda

Logger

Logback

Logback Architecture

Logback-classic-module

Configuration

Appenders
What is Loggers or Logging ?
Logging refers to the recording of activity.
Logging is a common issue for development
teams.
Logging Levels

TRACE

DEBUG

INFO

WARN

ERROR
What is Logback ???

Logback is intended as a successor to the popular log4j project.

Logback is divided into three modules
logback-core
logback-classic
logback-access

logback-core module lays the groundwork for the other two modules.

logback-classic natively implements the SLF4J API

logback-access module integrates with Servlet containers, such as
Tomcat and Jetty
Logback-classic-module

Built upon three main classes: Logger,
Appender and Layout

Logger → logback-classic

Appender & layout → logback-core
Logger Context

It is the logging space which is categorized according to developer-chosen
criteria.

Every single logger is attached to a LoggerContext.

It is responsible for arranging loggers in a tree like hierarchy.

Loggers are named entities which is case-sensitive.

They follow the hierarchical naming rule.
Named Hierarchy
A logger is said to be an ancestor of another logger if its name followed by a dot is
prefix of the descendant logger name.
A logger is said to be a parent of a child logger if there are no ancestors between
itself and the descendant logger.
"com.foo" is a parent of the logger named "com.foo.Bar".
"java" is a parent of "java.util" and an ancestor of "java.util.Vector".
Root Logger
The root logger resides at the top of the logger hierarchy. It is exceptional in that it is part of
every hierarchy at its inception. Like every logger, it can be retrieved by its name, as follows:
val rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME)
Other methods :
package org.slf4j;
public interface Logger {
// Printing methods:
public void trace(String message);
public void debug(String message);
public void info(String message);
public void warn(String message);
public void error(String message);
}
Effective Level

Loggers may be assigned levels

If a given logger is not assigned a level, then it inherits one from its closest
ancestor with an assigned level

The root logger always has an assigned level. By default, this is DEBUG.
The effective level for a given logger L, is equal to the first non-null level in its
hierarchy, starting at L itself and proceeding upwards in the hierarchy
towards the root logger.
Basic Selection Rule
A log request of level p issued to a logger having an effective level q, is enabled if p >=
q.
levels order: TRACE < DEBUG < INFO < WARN < ERROR.
Configuration

Logback tries to find a file called logback.groovy in the classpath.

If no such file is found, logback tries to find a file called logback-test.xml in the
classpath.

If no such file is found, it checks for the file logback.xml in the classpath..

If neither file is found, logback configures itself automatically using the
BasicConfigurator which will cause logging output to be directed to the console.
logback.xml
Printing status messages
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Automatically reloading configuration file
upon modification
<configuration scan="true">
...
</configuration>
For scan periods :
<configuration scan="true" scanPeriod="30 seconds" >
...
</configuration>
Configuration file syntax

Case sensitivity of tag names

Configuring loggers, or the <logger> element

Configuring the root logger, or the <root> element

Configuring Appenders
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="com.configuration" level="INFO" />
<logger name="chapters.configuration.Foo" level="DEBUG" />
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Appenders
Logback delegates the task of writing a logging event to components called
appenders Logback-core
Console Appender
OutputStreamAppender File Appender
Rolling File Appender
Time Based Size based Fixed Window
Console Appender
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
File Appender :
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
Shouldn't log everything.

Performance will be affected.

Take a long time to analyze important
information
Source :

http://logback.qos.ch/manual/introduction.html
Logging with Logback in Scala

Logging with Logback in Scala

  • 1.
    Logging with Logback inScala Anurag Srivastava Software Consultant Knoldus Software LLP
  • 2.
  • 3.
    What is Loggersor Logging ?
  • 4.
    Logging refers tothe recording of activity. Logging is a common issue for development teams.
  • 5.
  • 6.
  • 7.
     Logback is intendedas a successor to the popular log4j project.  Logback is divided into three modules logback-core logback-classic logback-access  logback-core module lays the groundwork for the other two modules.  logback-classic natively implements the SLF4J API  logback-access module integrates with Servlet containers, such as Tomcat and Jetty
  • 9.
    Logback-classic-module  Built upon threemain classes: Logger, Appender and Layout  Logger → logback-classic  Appender & layout → logback-core
  • 10.
    Logger Context  It isthe logging space which is categorized according to developer-chosen criteria.  Every single logger is attached to a LoggerContext.  It is responsible for arranging loggers in a tree like hierarchy.  Loggers are named entities which is case-sensitive.  They follow the hierarchical naming rule.
  • 11.
    Named Hierarchy A loggeris said to be an ancestor of another logger if its name followed by a dot is prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger. "com.foo" is a parent of the logger named "com.foo.Bar". "java" is a parent of "java.util" and an ancestor of "java.util.Vector".
  • 12.
    Root Logger The rootlogger resides at the top of the logger hierarchy. It is exceptional in that it is part of every hierarchy at its inception. Like every logger, it can be retrieved by its name, as follows: val rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME) Other methods : package org.slf4j; public interface Logger { // Printing methods: public void trace(String message); public void debug(String message); public void info(String message); public void warn(String message); public void error(String message); }
  • 13.
    Effective Level  Loggers maybe assigned levels  If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level  The root logger always has an assigned level. By default, this is DEBUG. The effective level for a given logger L, is equal to the first non-null level in its hierarchy, starting at L itself and proceeding upwards in the hierarchy towards the root logger.
  • 15.
    Basic Selection Rule Alog request of level p issued to a logger having an effective level q, is enabled if p >= q. levels order: TRACE < DEBUG < INFO < WARN < ERROR.
  • 16.
    Configuration  Logback tries tofind a file called logback.groovy in the classpath.  If no such file is found, logback tries to find a file called logback-test.xml in the classpath.  If no such file is found, it checks for the file logback.xml in the classpath..  If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
  • 17.
  • 18.
    Printing status messages <configurationdebug="true"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT" /> </root> </configuration>
  • 19.
    Automatically reloading configurationfile upon modification <configuration scan="true"> ... </configuration> For scan periods : <configuration scan="true" scanPeriod="30 seconds" > ... </configuration>
  • 20.
  • 21.
     Case sensitivity oftag names  Configuring loggers, or the <logger> element  Configuring the root logger, or the <root> element  Configuring Appenders
  • 22.
    <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern> %d{HH:mm:ss.SSS} [%thread]%-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <logger name="com.configuration" level="INFO" /> <logger name="chapters.configuration.Foo" level="DEBUG" /> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>
  • 23.
    Appenders Logback delegates thetask of writing a logging event to components called appenders Logback-core Console Appender OutputStreamAppender File Appender Rolling File Appender Time Based Size based Fixed Window
  • 24.
    Console Appender <appender name="STDOUT"class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> </encoder> </appender> File Appender : <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>testFile.log</file> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender>
  • 25.
  • 26.
     Performance will beaffected.  Take a long time to analyze important information
  • 27.