Friday 25 February 2022

Logback: OnConsoleStatusListener: Print all new incoming status messages on the console

‘OnConsoleStatusListener’ interface is used to print all the new incoming status messages to the console.

 

How to configure OnConsoleStatusListener?

There are three ways to configure OnConsoleStatusListener.

 

Way 1: Setting in logback.xml file.

<configuration>

	<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />

</configuration>

 

Way 2: Setting programmatically.

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); 
StatusManager statusManager = loggerContext.getStatusManager();
OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
statusManager.add(onConsoleListener);

 

Way 3: By setting system property logback.statusListenerClass.

java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener

 

Find the below working application.

 

Step 1: Create new maven project ‘logback-console-status-listener’.

 

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-console-status-listener</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 under src/main/resources folder.

logback.xml

<configuration>

	<statusListener
		class="ch.qos.logback.core.status.OnConsoleStatusListener" />

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

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

 

Step 4: Define HelloWorld class.

 

HelloWorld.java

package com.sample.app;

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

public class HelloWorld {

	public static void main(String[] args) {

		Logger logger = LoggerFactory.getLogger(HelloWorld.class);
		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, you will see below messages on console.

10:52:12,451 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
10:52:12,451 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/krishna/Documents/metacat/logback-console-status-listener/target/classes/logback.xml]
10:52:12,549 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
10:52:12,550 |-INFO in ch.qos.logback.core.joran.action.StatusListenerAction - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener]
10:52:12,610 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:52:12,616 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
10:52:12,624 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:52:12,641 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to WARN
10:52:12,641 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
10:52:12,641 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:52:12,643 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@79ad8b2f - Registering current configuration as safe fallback point
10:52:12.644 [main] WARN  com.sample.app.HelloWorld - Warning message
10:52:12.646 [main] ERROR com.sample.app.HelloWorld - Error message

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-console-status-listener

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment