Sunday 27 February 2022

Programatically configure a logback appender

Follow below step-by-step procedure to configure a logback appender programmatically.

 

Step 1: Get an instance of LoggerContext.

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

 

Step 2: Get an instance of PatternLayoutEncoder, set the pattern, context and start the encoder.

PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
patternLayoutEncoder.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
patternLayoutEncoder.setContext(loggerContext);
patternLayoutEncoder.start();

Step 3: Get an instance of FileAppender, set the file path, encoder, context and start the appender.

FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
fileAppender.setFile("/Users/Shared/logback/app.log");
fileAppender.setEncoder(patternLayoutEncoder);
fileAppender.setContext(loggerContext);
fileAppender.start();

Step 4: Get a Logger instance and attach the file appender to it.

Logger logger = (Logger) LoggerFactory.getLogger(HelloWorld.class);
logger.addAppender(fileAppender);
logger.setLevel(Level.INFO);
logger.setAdditive(false);

Find the below working application.

 

Step 1: Create new maven project ‘appender-via-program’.

 

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>appender-via-program</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 HelloWorld application.

 

HelloWorld.java

package com.sample.app;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
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 HelloWorld {

	public static void main(String[] args) {

		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

		PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
		patternLayoutEncoder.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
		patternLayoutEncoder.setContext(loggerContext);
		patternLayoutEncoder.start();

		FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
		fileAppender.setFile("/Users/Shared/logback/app.log");
		fileAppender.setEncoder(patternLayoutEncoder);
		fileAppender.setContext(loggerContext);
		fileAppender.start();

		Logger logger = (Logger) LoggerFactory.getLogger(HelloWorld.class);
		logger.addAppender(fileAppender);
		logger.setLevel(Level.INFO);
		logger.setAdditive(false);

		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 application, open app.log to see the log messages.

$pwd
/Users/Shared/logback
$
$ls
app.log
$
$cat app.log 
2022-02-25 09:52:49,433 INFO [main] c.s.a.HelloWorld [HelloWorld.java:36] Info message
2022-02-25 09:52:49,435 WARN [main] c.s.a.HelloWorld [HelloWorld.java:37] Warning message
2022-02-25 09:52:49,435 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:38] Error message

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/appender-via-program

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment