Saturday 26 February 2022

Logback: read environment variables from logback configuration file

In this post, I am going to explain how to read or refer environment variables from logback configuration file.

 

As per the logback documentation, properties defined in logback file are looked in local scope first, followed by context scope and finally check system properties. Local scope is the default scope.

 

Example

java -DLOG_FILES_HOME=/Users/Shared/logback MainClass

You can refer the system property LOG_FILES_HOME in logback.xml file like below.

<file>${LOG_FILES_HOME}/myApp.log</file>

Find the below working application.

 

Step 1: Create new maven project ‘read-env-variables-in-logback-config’.

 

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>read-env-variables-in-logback-config</artifactId>
	<version>1</version>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

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

	<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 java.io.IOException;

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

import ch.qos.logback.core.joran.spi.JoranException;

public class HelloWorld {
	private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

	public static void main(String[] args) throws JoranException, IOException {

		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.


Right click on HelloWorld.java -> Run As -> Run Configurations…

 

Go to Arguments tab.

 


 

 

Under VM arguments section add below text.

-DLOG_FILES_HOME=/Users/Shared/logback

 

Click on Run button. You can observe myApp.log file is created under /Users/Shared/logback folder.

$pwd
/Users/Shared/logback
$
$ls
myApp.log
$
$cat myApp.log 
2022-02-25 11:26:41,922 WARN [main] c.s.a.HelloWorld [HelloWorld.java:18] Warning message
2022-02-25 11:26:41,924 ERROR [main] c.s.a.HelloWorld [HelloWorld.java:19] Error message

 

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/logback/read-env-variables-in-logback-config

 

 
 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment