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 name="dev | prod">
	<!-- configuration to be enabled when the "dev" or "prod" profiles are active -->

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

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"?>
  <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">
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  <springProfile name="local | dev">
  	<logger name="org.springframework" level="DEBUG" additivity="false">
	    <appender-ref ref="CONSOLE" />
    <root level="DEBUG">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="FILE" />

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

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

3. Active spring profile

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


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;

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


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