Saturday 26 February 2022

Logback: attach custom filter to an appender

In this post, I am going to explain how to attach a custom filter to an appender.

 

Step 1: Define custom filter by extending AbstractMatcherFilter class.

public class InfoAndErrorFilter extends AbstractMatcherFilter {

	@Override
	public FilterReply decide(Object event) {
		if (!isStarted()) {
			return FilterReply.NEUTRAL;
		}

		LoggingEvent loggingEvent = (LoggingEvent) event;

		List<Level> eventsToKeep = Arrays.asList(Level.INFO, Level.ERROR);
		if (eventsToKeep.contains(loggingEvent.getLevel())) {
			return FilterReply.ACCEPT;
		} else {
			return FilterReply.DENY;
		}
	}

}

 

Above filter accept only INFO and ERROR messages.

 

Step 2: Attach the filter to an appender.

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

	<filter class="com.sample.app.filter.InfoAndErrorFilter" />

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

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-attach-custom-filter-to-appender’.

 

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-attach-custom-filter-to-appender</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: Define logback.xml file under 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="com.sample.app.filter.InfoAndErrorFilter" />

		<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" />
	</logger>


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

 

Step 4: Define InfoAndErrorFilter class.

 

InfoAndErrorFilter.java

package com.sample.app.filter;

import java.util.Arrays;
import java.util.List;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;

public class InfoAndErrorFilter extends AbstractMatcherFilter {

	@Override
	public FilterReply decide(Object event) {
		if (!isStarted()) {
			return FilterReply.NEUTRAL;
		}

		LoggingEvent loggingEvent = (LoggingEvent) event;

		List<Level> eventsToKeep = Arrays.asList(Level.INFO, Level.ERROR);
		if (eventsToKeep.contains(loggingEvent.getLevel())) {
			return FilterReply.ACCEPT;
		} else {
			return FilterReply.DENY;
		}
	}

}

 

Step 5: Define HelloWorld class.

 

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.

 

 


Total project structure looks like below.

12:53:45.578 [main] INFO  com.sample.app.HelloWorld - Info message
12:53:45.580 [main] WARN  com.sample.app.HelloWorld - Warning message
12:53:45.580 [main] ERROR com.sample.app.HelloWorld - Error message

 

Query the contents of d1.log to confirm filtering logic.

$pwd
/Users/Shared/logback
$
$ls
d1.log
$
$cat d1.log 
2022-02-23 12:53:45,578 INFO [main] c.s.a.HelloWorld [HelloWorld.java:13] Info message
2022-02-23 12:53:45,580 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-attach-custom-filter-to-appender

 

 


 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment