Saturday 26 February 2022

Logback: log different log level messages to different destinations

In this post, I am going to explain how to configure a logger to log different log level messages to different destinations.

 

For example, my logger is configured with log level INFO and 3 destinations are attached to the logger.

a.   Destination d1 is interested in only INFO, WARN messages.

b.   Destination d2 is interested in only WARN and ERROR messages

c.    Destination d3 want all the log messages.

 

We can achieve this scenario using filters.

 

Example

<appender name="d1" class="ch.qos.logback.core.FileAppender">
	<file>${LOG_FILES_HOME}/d1.log</file>

	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>DEBUG</level>
		<onMatch>DENY</onMatch>
	</filter>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>INFO</level>
		<onMatch>ACCEPT</onMatch>
	</filter>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>TRACE</level>
		<onMatch>DENY</onMatch>
	</filter>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>WARN</level>
		<onMatch>ACCEPT</onMatch>
	</filter>
	<filter class="ch.qos.logback.classic.filter.LevelFilter">
		<level>ERROR</level>
		<onMatch>DENY</onMatch>
	</filter>

	<encoder>
		<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
		</pattern>
	</encoder>
</appender>

 

Above snippet used to configure the appender d1.

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-filter-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml 

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>logback-filter-demo</artifactId>
	<version>1</version>

	<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>
</project>

 

Step 3 : Create logback.xml file in src/main/resources folder.

 

logback.xml

<configuration scan="true" scanPeriod="15 seconds">

	<property name="LOG_FILES_HOME" value="/Users/Shared/logback" />

	<appender name="d1" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_FILES_HOME}/d1.log</file>

		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>DENY</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>ACCEPT</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>TRACE</level>
			<onMatch>DENY</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>WARN</level>
			<onMatch>ACCEPT</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>DENY</onMatch>
		</filter>

		<encoder>
			<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
			</pattern>
		</encoder>
	</appender>

	<appender name="d2" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_FILES_HOME}/d2.log</file>

		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>DEBUG</level>
			<onMatch>DENY</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>INFO</level>
			<onMatch>DENY</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>TRACE</level>
			<onMatch>DENY</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>WARN</level>
			<onMatch>ACCEPT</onMatch>
		</filter>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
		</filter>

		<encoder>
			<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
			</pattern>
		</encoder>

	</appender>

	<appender name="d3" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_FILES_HOME}/d3.log</file>

		<encoder>
			<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
			</pattern>
		</encoder>
	</appender>


	<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.sample.app" level="INFO">
		<appender-ref ref="d1" />
		<appender-ref ref="d2" />
		<appender-ref ref="d3" />
	</logger>


	<root level="INFO">
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

 

Step 4: Define HelloWorld application.

 

HelloWorld.java

 

package com.sample.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
	private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

	public static void main(String[] args) {

		logger.trace("Trace message");
		logger.debug("Debug message");
		logger.info("Info message");
		logger.warn("Warning message");
		logger.error("Error message");

	}
}

 

Total project structure looks like below.



Run HelloWorld.java application, you will see below messages in console.

12:38:53.559 [main] INFO  com.sample.app.HelloWorld - Info message
12:38:53.561 [main] WARN  com.sample.app.HelloWorld - Warning message
12:38:53.562 [main] ERROR com.sample.app.HelloWorld - Error message

 

You can confirm the filtering by querying the content of d1, d2 and d3.log files.

$pwd
/Users/Shared/logback
$
$ls
d1.log	d2.log	d3.log
$
$cat d1.log 
2022-02-23 12:38:53,559 INFO [main] c.s.a.HelloWorld [HelloWorld.java:13] Info message
2022-02-23 12:38:53,561 WARN [main] c.s.a.HelloWorld [HelloWorld.java:14] Warning message
$
$cat d2.log 
2022-02-23 12:38:53,561 WARN [main] c.s.a.HelloWorld [HelloWorld.java:14] Warning message
2022-02-23 12:38:53,562 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:15] Error message
$
$cat d3.log 
2022-02-23 12:38:53,559 INFO [main] c.s.a.HelloWorld [HelloWorld.java:13] Info message
2022-02-23 12:38:53,561 WARN [main] c.s.a.HelloWorld [HelloWorld.java:14] Warning message
2022-02-23 12:38:53,562 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:15] Error message

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-filter-demo

 

 


 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment