Enterprise Java

Sample Logback Configuration for Spring Boot Profile Based Logging

We would want different logging configurations for different profiles in Spring Boot, like in local running we would just want console logging and for production, we would want file logging with support for rolling the log files daily.

I came up with a sample logback configuration which I use in all my applications. Create a file named logback-spring.xml in src/main/resources with the following content:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <include resource="org/springframework/boot/logging/logback/defaults.xml" />
  <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}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">
    <root level="INFO">
      <appender-ref ref="CONSOLE" />
      <appender-ref ref="FILE" />
    </root>
  </springProfile>
  <springProfile name="test,prod">
    <root level="INFO">
      <appender-ref ref="FILE" />
    </root>
  </springProfile>
 
</configuration>

We are using the default console appender which is provided by Spring Boot but providing our own daily rolling based file appender. I have mostly copied the base.xml and updated it to meet my needs.

Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: Sample Logback Configuration for Spring Boot Profile Based Logging

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Raghu
Raghu
6 years ago

can u put what are the dependencies needed for logback-spring.xml

Back to top button