【问题标题】:How can I use YAML to configure multiple log files for Logback/Springboot?如何使用 YAML 为 Logback/Springboot 配置多个日志文件?
【发布时间】:2018-03-27 05:08:57
【问题描述】:

我正在重写一个小的 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 yaml logback dropwizard


【解决方案1】:

Spring Boot 的 Logback 的 YAML 配置只允许单个文件 appender。为了将 Logback 配置为使用多个文件附加程序,您必须提供明确的 logback.xmllogback-spring.xml。如果您从 application.yaml 文件中删除日志记录配置,然后将 logback.xmllogback-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>

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2016-06-09
    • 2019-12-16
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多