我正在重写一个小型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应用程序相同的结果?
答案 0 :(得分:1)
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>