logging with logback in scala
TRANSCRIPT
PowerPoint Presentation
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-corelogback-classiclogback-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
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
Automatically reloading configuration file upon modification
...
For scan periods :
...
Configuration file syntax
Case sensitivity of tag names
Configuring loggers, or the element
Configuring the root logger, or the element
Configuring Appenders
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
Appenders
Logback delegates the task of writing a logging event to components called appenders Logback-core Console AppenderOutputStreamAppender File Appender
Rolling File Appender
Time Based Size based Fixed Window
Console Appender
%-4relative [%thread] %-5level %logger{35} - %msg %n
File Appender :
testFile.log %-4relative [%thread] %-5level %logger{35} - %msg%n
Shouldn't log everything.
Performance will be affected.
Take a long time to analyze important information
Source :
http://logback.qos.ch/manual/introduction.html