Spring boot profile specific logging example

Spring boot provides support for separate logging configurations for separate runtime environments/profiles. E.g. we can use certain log configuration at development and other configuration at production environment.

Environment or profile specific logging configuration is only possible using logback. Other logging providers (e.g. log4j2) does not support it.

1. Syntax – springProfile tag

Create logback-spring.xml file in '/resources' folder and use <springProfile> tags to provide profile specific logging configurations.

springProfile tag lets us optionally include or exclude sections of configuration based on the active Spring profiles. It’s name attribute allows a simple profile name or complex profile expression.

<springProfile name="prod">
	<!-- configuration to be enabled when the "prod" profile is active -->
</springProfile>

<springProfile name="dev | prod">
	<!-- configuration to be enabled when the "dev" or "prod" profiles are active -->
</springProfile>

<springProfile name="!prod">
	<!-- configuration to be enabled when the "prod" profile is not active -->
</springProfile>

2. Profile specific logging example

In given logging configuration, we want to use console and file logging in only local and dev environments. In elevated environments (e.g. prod), we want to use only file logging.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <property name="LOG_FILE" value="c:/temp/spring.log}"/>
  <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
 
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <file>${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOG_FILE}.%d</fileNamePattern>
    </rollingPolicy>
  </appender>
 
  <springProfile name="local | dev">
  	<logger name="org.springframework" level="DEBUG" additivity="false">
	    <appender-ref ref="CONSOLE" />
	</logger>
    <root level="DEBUG">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="FILE" />
    </root>
  </springProfile>

  <springProfile name="prod">
    <root level="INFO">
      <appender-ref ref="FILE" />
    </root>
  </springProfile>

  <springProfile name="!local & !dev & !prod">
    <root level="INFO">
      <appender-ref ref="FILE" />
    </root>
  </springProfile>
 
</configuration>

3. Active spring profile

To activate a particular spring profile, we can either specify it in application.properties.

spring.profiles.active=dev

Or we can pass it as runtime argument while starting the spring boot application as jar file.

$ java -jar \target\my-app.jar -Dspring.profiles.active=dev

Note: Make sure to pass the application start arguments (args) to SpringApplication.run() method.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class Application 
{
	public static void main(String[] args) 
	{
		SpringApplication.run(Application.class, args);
		
		log.info("Simple log statement with inputs {}, {} and {}", 1, 2, 3);
	}
}

Drop me your questions related to logback profile specific configuration in spring boot.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.