Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use YAML to configure multiple log files for Logback/Springboot?

I'm rewriting a small DropWizard application to run on SpringBoot.

My DW app has the following logging config which works:

logging:
  level: INFO
  appenders:
    - type: file
      currentLogFilename: /var/log/paas/console.log
      archivedLogFilenamePattern: /var/log/paas/console.log-%d.gz
      archivedFileCount: 7

  loggers:
    com.myorg:
      level: DEBUG
      appenders:
        - type: file
          currentLogFilename: /var/log/paas/paas.log
          archivedLogFilenamePattern: /var/log/paas/paas.log-%d.gz
          archivedFileCount: 7

This config separates my application and console messages into two separate logs.

When I try using this same configuration with SpringBoot, it has no effect. I'm able to write everything to a single log with the following config, but I really need to have two separate logs:

logging:
  level:
    org.springframework.web: INFO
    com.myorg: DEBUG
  file: /var/log/paas/paas.log

Is it not possible to do this with LogBack and YAML? Or is there an alternate syntax that will give me the same result I'm getting for my DropWizard app?

like image 706
Brenda Bell Avatar asked Oct 15 '17 16:10

Brenda Bell


1 Answers

Spring Boot's YAML configuration of Logback only allows a single file appender. In order to configure Logback to use multiple file appenders you'll have to provide an explicit logback.xml or logback-spring.xml. If you remove the logging confoguraiotn from your application.yaml file and then add either a logback.xml or a logback-spring.xml to the root of your runtime classpath then Logback will be configured from that file.

Here's an example, using a logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- deny all events with a level below INFO, that is TRACE and DEBUG -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <file>/var/log/paas/console.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/var/log/paas/console.log-%d.gz</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>...</pattern>
        </encoder>
    </appender>

    <appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/paas/paas.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>/var/log/paas/paas.log-%d.gz</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>...</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="INFO_FILE"/>
        <appender-ref ref="DEBUG_FILE"/>
    </root>

</configuration>
like image 159
glytching Avatar answered Sep 22 '22 17:09

glytching