0

我在我的 Spring Boot 应用程序中使用下面的 logback-spring.xml 文件。在 xml 文件中启用了特定于配置文件的配置。但是当我的环境进行测试时,它会在 example-wsrest_test.log 文件中写入条目,但随后它还会在服务器启动时创建另外两个日志文件 example-wsrest_prod.log 和 example-wsrest_acc.log。是否可以根据配置文件仅创建单个日志文件。

<!-- <include resource="org/springframework/boot/logging/logback/base.xml" 
    /> -->
<!-- use Spring default values @see https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />

<!-- <property name="LOG_PATH" value="." /> -->
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<property name="LOG_PATH_PROD" value="${LOG_PATH}" />
<property name="LOG_PATH_TEST" value="${LOG_PATH}" />
<property name="LOG_PATH_ACC" value="${LOG_PATH}" />
<property name="LOG_ARCHIVE_PROD" value="${LOG_PATH_PROD}/archive" />
<property name="LOG_ARCHIVE_TEST" value="${LOG_PATH_TEST}/archive" />

<property name="GLOBAL_TEST_LOG_LEVEL" value="INFO" />
<property name="GLOBAL_ACC_LOG_LEVEL" value="INFO" />
<property name="GLOBAL_PROD_LOG_LEVEL" value="ERROR" />
<property name="LOG_FILE_NAME" value="example-wsrest" />

<appender name="RollingFile-Appender-acc"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH_ACC}/${LOG_FILE_NAME}_acc.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>${LOG_PATH_ACC}/${LOG_FILE_NAME}_acc_%d{dd-MM-yyyy}_%i.log</fileNamePattern>
    <maxFileSize>10MB</maxFileSize>
    <maxHistory>30</maxHistory>
    <totalSizeCap>300MB</totalSizeCap>
    </rollingPolicy>    
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
</appender>

<appender name="RollingFile-Appender-prod"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH_PROD}/${LOG_FILE_NAME}_prod.log</file>     
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>${LOG_PATH_PROD}/${LOG_FILE_NAME}_prod_%d{dd-MM-yyyy}_%i.log</fileNamePattern>
    <maxFileSize>10MB</maxFileSize>
    <maxHistory>30</maxHistory>
    <totalSizeCap>300MB</totalSizeCap>
</rollingPolicy>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
</appender>

<appender name="RollingFile-Appender-test"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH_TEST}/${LOG_FILE_NAME}_test.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <fileNamePattern>${LOG_PATH_TEST}/${LOG_FILE_NAME}_test_%d{dd-MM-yyyy}_%i.log.zip</fileNamePattern>
    <maxFileSize>10MB</maxFileSize>
    <maxHistory>30</maxHistory>
    <totalSizeCap>300MB</totalSizeCap>
</rollingPolicy>
    <encoder>
        <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
</appender>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        <charset>utf8</charset>
    </encoder>
</appender>

<springProfile name="test">
    <root level="${GLOBAL_TEST_LOG_LEVEL}">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="RollingFile-Appender-test" />
    </root>
    <logger name="org.springframework.security" level="${GLOBAL_TEST_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
    <logger name="org.springframework.web" level="${GLOBAL_TEST_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
    <logger name="org.hibernate" level="${GLOBAL_TEST_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
</springProfile>

<springProfile name="acc">
    <root level="${GLOBAL_ACC_LOG_LEVEL}">
        <appender-ref ref="RollingFile-Appender-acc" />
    </root>
    <logger name="com.example.test" level="DEBUG"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-acc" />
    </logger>
  <logger name="org.springframework.security" level="${GLOBAL_ACC_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
    <logger name="org.springframework.web" level="${GLOBAL_ACC_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>
    <logger name="org.hibernate" level="${GLOBAL_ACC_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="CONSOLE" />
    </logger>

</springProfile>

<springProfile name="prod">
    <root level="${GLOBAL_PROD_LOG_LEVEL}">
        <appender-ref ref="RollingFile-Appender-prod" />
    </root>
    <!-- To know when the application is ready -->
    <logger name="com.example.test" level="INFO"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <!-- To know about the API endpoints scanned, found & available -->
    <logger name=" org.springframework.data.rest" level="INFO"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <!-- To know when and if the application is started on th eembedded Tomcat -->
    <logger name="org.springframework.boot.context.embedded.tomcat"
        level="INFO" additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <!-- To know about filters set, amongst other things -->
    <logger name="org.springframework.data.rest.webmvc" level="INFO"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <logger name="com.example.test" level="INFO"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <logger name="org.springframework.security" level="${GLOBAL_PROD_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <logger name="org.springframework.web" level="${GLOBAL_PROD_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
    <logger name="org.hibernate" level="${GLOBAL_PROD_LOG_LEVEL}"
        additivity="false">
        <appender-ref ref="RollingFile-Appender-prod" />
    </logger>
</springProfile>

4

1 回答 1

2

您必须在spring profile tag中定义 appender 。配置标签内的所有附加程序都已初始化,但日志仅基于弹簧配置文件写入文件。但是要每个配置文件有一个日志文件,每个配置文件都应该有自己的附加程序。下面的示例可能会有所帮助:

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

    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <springProfile name="test">
        <appender name="FILE_APPENDER_TEST" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
                <charset>utf8</charset>
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>%d{yyyy-MM-dd}-test.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
        </appender>
        <root level="TRACE">
            <appender-ref ref="FILE_APPENDER_TEST"/>
        </root>
    </springProfile>

    <springProfile name="prod">
        <appender name="FILE_APPENDER_PROD" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>%d{yyyy-MM-dd}-prod.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>${FILE_LOG_PATTERN}</pattern>
                <charset>utf8</charset>
            </encoder>
        </appender>
        <root level="INFO">
            <appender-ref ref="FILE_APPENDER_PROD"/>
        </root>
    </springProfile>

</configuration>



于 2019-08-09T09:51:22.200 回答