Friday, 25 February 2022

Introduction to logback

 

logback is a popular Java logging library (it is successor to the popular log4j project)

 

Modules

At the time of writing this post, logback is primarily divided into three modules.

a.   logback-core,

b.   logback-classic and

c.    logback-access

 

‘logback-core’ is the base module for all the other two modules. ‘logback-classic’ module extends logback-core module and provide a significant improvements on log4j module.

 

‘logback-access’ module integrates with Servlet containers to provide HTTP-access log functionality.

 

Core classes

There are three main classes in logback library.

a.   Logger

b.   Appender

c.    Layout

 

Appender and Layout interfaces are part of logback-core module, whereas Logger class is part of logback-classic module. Application interact with Logger instance to log messages. Appenders are used to log messages to different destinations. Layout instance convert incoming log event to a string.

 

Name hierarchy of loggers

A logger 'L1' is an ancestor of another logger 'L2' if its name followed by a dot is a prefix of the 'L2' logger name.

 

A logger 'L1' is a parent of a child logger 'L2' if there are no ancestors between 'L1' and the 'L2' logger.

 

For example, "java" is a parent of "java.util" and an ancestor of "java.util.ArrayList".

 

Logging levels

Logger can be assigned levels. Following levels are supported by most of the logging libraries.

a.   TRACE

b.   DEBUG

c.    INFO

d.   WARN and

e.   ERROR

 

'ch.qos.logback.classic.Level' defined the logging levels.

 


 

What if I do not set the logging level of a logger?

It inherits the logging level from its closest ancestor. For example, if you do not set logging level to the logger L1, it inherit the first non-null logging level in its naming hierarchy.

 

Logback root logger is assigned with DEBUG logging level by default.

 

Dependencies used

<dependencies>

	<dependency>
		<groupId>ch.qos.logback</groupId>
		<artifactId>logback-classic</artifactId>
		<version>1.2.10</version>
	</dependency>


	<dependency>
		<groupId>ch.qos.logback</groupId>
		<artifactId>logback-core</artifactId>
		<version>1.2.10</version>
	</dependency>

	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>1.7.36</version>
	</dependency>

</dependencies>

 

  


Previous                                                 Next                                                 Home

No comments:

Post a Comment