Saturday 26 February 2022

Variable or property substitution in a Logback file

Logback support variable substitution functionality in the configuration file.

 

How to define a variable in logback.xml file?

<property name="myVar" value="valueOfTheVar" />

 

How to refer the value of variable myVar?

${myVar}

 

Example

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

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

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

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

	.......
	.......
</configuration>

 

As you see in the above snippet, I set the variable "LOG_FILES_HOME" and using it in FILE appender.

 

Let’s see it with an example.

 

Step 1: Create new maven project ‘logback-variable-substitution’.

 

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-variable-substitution</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 scan="true" scanPeriod="15 seconds">

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

	<appender name="FILE" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_FILES_HOME}/myApp.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="WARN">
		<appender-ref ref="FILE" />
	</logger>


	<root level="INFO">
		<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 {
	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, you will see below messages in console.

15:46:38.568 [main] WARN  com.sample.app.HelloWorld - Warning message
15:46:38.570 [main] ERROR com.sample.app.HelloWorld - Error message

 

You can observe the same in myApp.log file.

$pwd
/Users/Shared/logback
$
$cat myApp.log 
2022-02-22 15:46:38,568 WARN [main] c.s.a.HelloWorld [HelloWorld.java:14] Warning message
2022-02-22 15:46:38,570 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:15] Error message

You can even set the variable by setting the system property like below.

java -DLOG_FILES_HOME="/Users/Shared/logback" HelloWorld

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/logback-variable-substitution


 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment