spring-boot – 如何使用YAML为Logback / Springboot配置多个日志文件?

我正在重写一个小的DropWizard应用程序以在
SpringBoot上运行.

我的DW应用程序具有以下日志配置:

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

此配置将我的应用程序和控制台消息分成两个单独的日志.

当我尝试在SpringBoot中使用相同的配置时,它没有任何效果.我可以使用以下配置将所有内容写入单个日志,但我确实需要两个单独的日志:

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

用LogBack和YAML做不到这个吗?或者是否有一种替代语法可以为我提供与DropWizard应用程序相同的结果?

最佳答案 Spring Boot的Logback的YAML配置只允许单个文件追加器.为了将Logback配置为使用多个文件追加器,您必须提供显式的logback.xml或logback-spring.xml.如果从application.yaml文件中删除日志记录confoguraiotn,然后将logback.xml或logback-spring.xml添加到运行时类路径的根目录,则将从该文件配置Logback.

这是一个使用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>
点赞