Spring Boot Logging to Multiple Profiles

Spring boot provides support for separate logging configurations for separate runtime environments/profiles. E.g. we can use certain log configurations at development and other configurations 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 using <springProfile> Tag

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

The 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 the given logging configuration, we want to use the 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. Activating a 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 a runtime argument while starting the spring boot application as the 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 !!

Comments

Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode