Spring-Boot externalize logback configuration file (logback-spring.xml)

Many times there is a need to externalize the logging configuration file in spring boot, so that without making any code level/ jar level changes we can easily change the logging configuration. For example If we deliver any spring boot application as a jar we dont want the logging file name to be hardcoded and to be present on any specific location, so If we have an opportunity to change the logging configuration file we can easily set the logging file location or name. Or take another example where we want to change the logging level or add logger for a new package we can easily do it if logging configuration file is externalized.

Lets see how we can externalize logback loggin configuration file -
Spring boot by default uses logback for logging and If we don't provide any configuration file It uses default configuration and prints logs in console. So if we want to make changes to the logback configuration we will have to create a logbacl configuration file.
Follow below steps to create a logback logging configuration file and externalize it -
  1. Create a xml configuration file for logback with name - logback-spring.xml, It is the preferred name by spring boot.
  2. Place It at the desired location on filesystem.
  3. While running the application using java command use --logging.config= to specify the location of this file.
    Check below example -
    java -jar demo-0.0.1-SNAPSHOT.jar --logging.config=../../logback-spring.xml
    Above example has been run form target folder and It will read the logback-spring.xml file from folder two level above the target folder/current directory.
Note: Make sure that no other logback configuration file is present in classpath If you want to use external configuration. You can remove any other classpath logging configuration file during build using build plugin in maven pom.xml (If maven is used).
Check sample logback-spring.xml file below-

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<include
		resource="org/springframework/boot/logging/logback/default.xml" />
	<include
		resource="org/springframework/boot/logging/logback/console-appender.xml" />
	<property name="LOG_PATH" value="/home/user/demo/log"/>
	<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>
	<appender name="FILE"
		class="ch.qos.logback.core.FileAppender">
		<file>${LOG_PATH}/demo.log</file>
		<encoder
			class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<Pattern>
				%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
			</Pattern>
		</encoder>
	</appender>
	<root level="TRACE">
		<appender-ref ref="FILE" />
		<appender-ref ref="CONSOLE" />
	</root>
</configuration>
A demo.log logging file will be created at location - /home/user/demo/log.
Hope this article will help you, In case of any query feel free to add a comment.
Thanks!

Comments

Popular posts from this blog

Create Height Balanced BST from Sorted Linkedlist

Reverse LinkedList in K nodes group - Data Structure Linked List

Finding Height Of A Binary Tree

Spring Cloud - Configuration Server with Git Integration