1

我正在处理 Spring LogBack 配置,因为我将有 2 个日志文件(info.logerror.log分别只有 INFO 日志和只有 ERROR 日志)。

实际上,当我运行应用程序时,会创建 2 个文件,但error.log是空的,并且没有记录 ERROR 日志。

如果我的配置文件是下面的,有什么问题?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />

<!-- APPENDERS DEFINITION -->
<appender name="LOG_INFO"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/myProject/info.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>/myProject/info.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>50MB</maxFileSize>
        <maxHistory>30</maxHistory>
        <totalSizeCap>500MB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>
            %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>


<appender name="LOG_ERROR"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/myProject/error.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>/myProject/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <maxFileSize>50MB</maxFileSize>
        <maxHistory>30</maxHistory>
        <totalSizeCap>500MB</totalSizeCap>
    </rollingPolicy>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
        <pattern>
            %d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<!--START ASYNC APPENDERS DEFINITION -->
    <appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="LOG_INFO" />
    </appender>
    <appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="LOG_ERROR" />
    </appender>
<!--END ASYNC APPENDERS DEFINITION -->

<!--START LOCAL CONFIGURATION -->
<springProfile name="local">    
    <logger name="my.package" level="INFO"  additivity="false" >
        <appender-ref ref="ASYNC_LOG_INFO" />
        <appender-ref ref="ASYNC_LOG_ERROR" />
    </logger>
</springProfile>
<!-- END LOCAL CONFIGURATION -->

</configuration>
4

0 回答 0