HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot 2 / Spring boot profile specific logging example

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Spring Boot Logging

  • Spring Boot – Guide to Logging
  • Spring Boot – log4j2.properties
  • Spring Boot – log4j2.xml
  • Spring Boot – application.yml
  • SB – application.properties
  • Spring Boot – Console log
  • Spring Boot – Performance Log
  • Spring Boot – Multiple log files
  • Spring Boot – Embed server logs
  • SB – Active profile logging
  • SB – Logging with Lombok

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces