Spring boot multiple log files example

Learn to create multiple log files in spring boot applications using multiple file appenders. Learn to configure all file appenders with rollover policies, archiving etc. wiih log4j2 and logback configurations.

1. Multiple log files with logback

The following logback.xml file contains 5 loggers. We can create more loggers as per our needs.

  • console – Logs output to console. Include all logs statements.
  • applicationLog – Logs output to application.log. Includes application debug logs for package com.howtodoinjava.demo.
  • aopLog – Logs output to application-aop.log. Includes application info logs in package com.howtodoinjava.demo.aop.
  • springLog – Create spring-framework.log and contains all error logs generated by spring framework.
  • hibernateLog – Create database.log and contains all error logs generated by hibernate.

Put the given logback.xml in resources folder.

<configuration>

	<property name="LOG_PATTERN" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n" />
	<property name="APP_LOG_ROOT" value="c:/temp"/>
	
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    	<encoder>
      		<pattern>${LOG_PATTERN}</pattern>
    	</encoder>
  	</appender>

	<appender name="applicationLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${APP_LOG_ROOT}/application.log</file>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
	        <fileNamePattern>${APP_LOG_ROOT}/application-%i.log</fileNamePattern>
	        <minIndex>1</minIndex>
	        <maxIndex>10</maxIndex>
	    </rollingPolicy>
	    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
	        <maxFileSize>10MB</maxFileSize>
	    </triggeringPolicy>
	</appender>
	
	<appender name="aopLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${APP_LOG_ROOT}/application-aop.log</file>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
	        <fileNamePattern>${APP_LOG_ROOT}/application-aop-%i.log</fileNamePattern>
	        <minIndex>1</minIndex>
	        <maxIndex>10</maxIndex>
	    </rollingPolicy>
	    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
	        <maxFileSize>10MB</maxFileSize>
	    </triggeringPolicy>
	</appender>  
	
	<appender name="springLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${APP_LOG_ROOT}/spring-framework.log</file>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
	        <fileNamePattern>${APP_LOG_ROOT}/spring-framework-%i.log</fileNamePattern>
	        <minIndex>1</minIndex>
	        <maxIndex>10</maxIndex>
	    </rollingPolicy>
	    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
	        <maxFileSize>10MB</maxFileSize>
	    </triggeringPolicy>
	</appender>  
	
	<appender name="hibernateLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${APP_LOG_ROOT}/database.log</file>
		<encoder>
			<pattern>${LOG_PATTERN}</pattern>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
	        <fileNamePattern>${APP_LOG_ROOT}/database-%i.log</fileNamePattern>
	        <minIndex>1</minIndex>
	        <maxIndex>10</maxIndex>
	    </rollingPolicy>
	    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
	        <maxFileSize>10MB</maxFileSize>
	    </triggeringPolicy>
	</appender>  
	
	<logger name="com.howtodoinjava.demo" level="DEBUG">
		<appender-ref ref="applicationLog" />
		<appender-ref ref="console" />
	</logger>
	
	<logger name="com.howtodoinjava.demo.aop" level="INFO">
		<appender-ref ref="aopLog" />
		<appender-ref ref="console" />
	</logger>
	
	<logger name="org.springframework" level="DEBUG">
		<appender-ref ref="springLog" />
		<appender-ref ref="console" />
	</logger>
	
	<logger name="org.hibernate" level="DEBUG">
		<appender-ref ref="hibernateLog" />
		<appender-ref ref="console" />
	</logger>

	 <root level="info">
	   	<appender-ref ref="console" />
	 </root>
	 
</configuration>

2. Multiple log files with log4j2

To achieve the above logging configuration with log4j2, include log4j2 dependency in classpath.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

Now add log4j2.xml in resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
 
    <Properties>
        <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Property name="APP_LOG_ROOT">c:/temp</Property>
    </Properties>
 
    <Appenders>
 
        <Console name="console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
         
        <RollingFile name="applicationLog" fileName="${sys:APP_LOG_ROOT}/application.log"
            filePattern="${sys:APP_LOG_ROOT}/application-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="springLog" fileName="${sys:APP_LOG_ROOT}/spring-framework.log"
            filePattern="${sys:APP_LOG_ROOT}/spring-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
        <RollingFile name="aopLog" fileName="${sys:APP_LOG_ROOT}/application-aop.log"
            filePattern="${sys:APP_LOG_ROOT}/application-aop-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
        
        <RollingFile name="hibernateLog" fileName="${sys:APP_LOG_ROOT}/database.log"
            filePattern="${sys:APP_LOG_ROOT}/spring-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="19500KB" />
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
         
    </Appenders>
 
    <Loggers>
     
     	<Logger name="com.howtodoinjava.demo" additivity="false" level="debug">
            <AppenderRef ref="applicationLog" />
            <AppenderRef ref="console"  />
        </Logger>
        
        <Logger name="com.howtodoinjava.demo.aop" additivity="false" level="info">
            <AppenderRef ref="aopLog" />
            <AppenderRef ref="console" />
        </Logger>
         
        <Logger name="org.springframework" additivity="false" level="error">
            <AppenderRef ref="springLog" />
            <AppenderRef ref="console"/>
        </Logger>
        
        <Logger name="org.hibernate" additivity="false" level="error">
            <AppenderRef ref="hibernateLog" />
            <AppenderRef ref="console"/>
        </Logger>
                 
        <Root level="INFO">
            <AppenderRef ref="console"/>
        </Root>
 
    </Loggers>
 
</Configuration>

3. Spring boot multiple log files demo

Run the application with any of the given configurations above. You will see the log files generated in 'c:/temp' folder.

Spring boot multiple log files example
Spring boot multiple log files example

Drop me your questions related to configuring spring boot logging with multiple file appenders.

Happy Learning !!

Leave a Reply

2 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