Spring boot logging into multiple files

spring boot logging best practices
dynamic log file name spring boot
spring boot logback
spring boot slf4j
spring boot log4j
spring boot logback.xml location
logging.file.max-size spring boot example
spring boot log4j properties

Is there any way I can configure spring boot logging to multiple files/console based on the configuration? i.e Some of the log statements should write into an audit file and normal log statements should go to console/normal log file.

Below is the code I have tried on spring boot example application.

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="file" level="DEBUG" additivity="false">
        <appender-ref ref="FILE" />
    </logger>
    <logger name="org.hello" level="ERROR" additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
</configuration>

Below is the application.properties entries

logging.level.org.springframework.web=INFO
logging.file=logs/spring-boot-logging.log

Below the HelloController

@RestController
public class HelloController {

Logger logger = LoggerFactory.getLogger(HelloController.class);

    Logger logger1 = LoggerFactory.getLogger("file");

    @RequestMapping("/")
    public String index() {

        logger.info("My Log test");
        logger1.info("My Audit test");


        return "Greetings from Spring Boot!";
    }

}

Can someone please help? Did anyone face similar situation?

Thanks a lot

Standard logback example, two files with different packages going to different files :

<configuration>

  <appender name="FILE1" class="ch.qos.logback.core.FileAppender">
    <file>myApp1.log</file>
    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE2" class="ch.qos.logback.core.FileAppender">
    <file>myApp1.log</file>
    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>

  <logger name="com.package1.foo" level="DEBUG">
    <appender-ref ref="FILE1" />
  </logger>

  <logger name="com.package2.bar" level="DEBUG">
    <appender-ref ref="FILE2" />
  </logger>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Spring boot multiple log files example, Learn to create multiple log files in spring boot applications using multiple file appenders. 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.

Just add another logger and appender in your configuration file and create logger object using the name of Logger defined in the configuration. For more explanation check Spring boot multiple log files

Logging in Spring Boot, Setting the Log Level in Spring Boot when Testing a fragment of a Logback configuration file in which we set the level for two separate packages: we need to turn it into a skinny version, and (only once) add our alternative  Log4j2 Logging Step 1: Exclude logback and include log4j2 As mentioned earlier, spring boot uses logback as default. So if we have to Step 2: Add log4j2 configuration file Now, add log4j2 specific configuration file in classpath (typically in resources Step 3: With or without Slf4j By default,

Spring boot logging into multiple files and we can create log into selected class also. I used the following logback.xml

<configuration>
<appender name="logOutPut" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/WebServe-logback.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>${catalina.base}/logs/WebServe-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>50MB</maxFileSize>    
        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="errorlab" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/error-logback.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <fileNamePattern>${catalina.base}/logs/error-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>50MB</maxFileSize>    
        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="jdbc.sqltiming.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/WebServe-sql-timing.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">       
        <fileNamePattern>${catalina.base}/logs/WebServe-sql-timing-log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>50MB</maxFileSize>    
        <maxHistory>30</maxHistory>
        <totalSizeCap>1GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<logger name="com.tasks.MailNotificationTask">
   <level value="ALL" />
   <appender-ref ref="errorlab" />
</logger>

<logger name="jdbc.sqltiming">
    <appender-ref ref="jdbc.sqltiming.appender" />
</logger>

<root level="debug">
    <appender-ref ref="logOutPut" />
</root>

Dependency :

     <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>

    <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>

Configuring Logback with Spring Boot, Logback is provided out of the box with Spring Boot when you use RollingFileAppender will save the logs to different files depending on their rolling policy. This is handy as it allows the log output to be split out into various  That's because every starter, like our spring-boot-starter-web, depends on spring-boot-starter-logging, which already pulls in spring-jcl for us. When using starters, Logback is used for logging by default. Spring Boot pre-configures it with patterns and ANSI colors to make the standard output more readable.

Configuring Logback With Spring Boot, This is handy, as it allows the log output to be split out into various forms that you have control over. For example, you could separate the log files  If we choose to configure multiple appenders, we can also separate SQL statements and other log statements into different log files to keep things clean. Generic bottom I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Get Started Quickly With Spring Boot Logging, Before we delve into logging and Spring Boot, we need a demo store the Logback configuration in a separate file named logback-spring.xml. In this tutorial, we will show you how to use Logback Mapped Diagnostic Context (MDC) and SiftingAppender to create a separate log file for each thread.. P.S Tested with Logback 1.1.2, should work in earlier version.

26. Logging, 26.3 File Output. By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in  This tutorial will show how to load multiple configuration files from different directories with Spring Boot. By default, Spring Boot look for your externalized configuration file (application. proroperties ) in four predetermined locations : in classpath root, in the package / config in classpath, in the current directory