Friday 25 February 2022

Logback: set Appender additivity programatically

The output of a log statement of logger L1 will go to all the appenders of L1 and its ancestors.

 

Let’s see it with an example.

 

AppenderAdditivity.java

package com.sample.app;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;

public class AppenderAdditivity {

	private static Logger getLogger(String name) {
		Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);

		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		PatternLayoutEncoder ple = new PatternLayoutEncoder();

		ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
		ple.setContext(loggerContext);
		ple.start();
		FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
		fileAppender.setFile("/Users/Shared/logback/" + name + ".log");
		fileAppender.setEncoder(ple);
		fileAppender.setContext(loggerContext);
		fileAppender.start();

		logger.addAppender(fileAppender);

		return logger;
	}

	public static void main(String[] args) {

		Logger loggerA = getLogger("A");
		Logger loggerADotB = getLogger("A.B");
		Logger loggerADotBDotC = getLogger("A.B.C");

		loggerADotBDotC.info("Hello world....");
		
	}
}

 

As you see above snippet, I defined 3 loggers with name

a.   A

b.   A.B

c.    A.B.C

A file appender is attached to each of these loggers. When I write message to A.B.C logger, it is delivered to the appenders of A.B.C, A.B, A.

 

When you ran above application, you can observe A.log, A.B.log, A.B.C.log files are created in /Users/Shared/logback folder.

 


How to disable additivity of a logger?

Appender additivity is enabled by default. You can disabled this using setAdditivity method.

 

Example

logger.setAdditive(false);

 

AppenderAdditivity.java

package com.sample.app;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;

public class AppenderAdditivity {

	private static Logger getLogger(String name) {
		Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);

		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		PatternLayoutEncoder ple = new PatternLayoutEncoder();

		ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
		ple.setContext(loggerContext);
		ple.start();
		FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
		fileAppender.setFile("/Users/Shared/logback/" + name + ".log");
		fileAppender.setEncoder(ple);
		fileAppender.setContext(loggerContext);
		fileAppender.start();

		logger.addAppender(fileAppender);
		
		// Comment below lin if you want to enable appender additivity
		logger.setAdditive(false);

		return logger;
	}

	public static void main(String[] args) {

		Logger loggerA = getLogger("A");
		Logger loggerADotB = getLogger("A.B");
		Logger loggerADotBDotC = getLogger("A.B.C");

		loggerADotBDotC.info("Hello world....");
		
	}
}

 

Output

  


From the above image, you can confirm that the messages are not passed to parent appenders.

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment