Spring Boot logback springProfile 区分部署环境

logback Spring Boot About 2,485 words

springProfile

读取spring.profiles.active设置的值,设置不同环境的不同逻辑。

固定值

dev:当spring.profiles.activedev时生效。

<springProfile name="dev">
    <appender-ref ref="CONSOLE"/>
</springProfile>

| 或

|:当spring.profiles.activedevtest时生效。

<springProfile name="dev | test">
    <appender-ref ref="CONSOLE"/>
</springProfile>

! 非

!:当spring.profiles.active不是prod时生效。

<springProfile name="!prod">
    <appender-ref ref="CONSOLE"/>
</springProfile>

示例

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

    <springProperty scope="context" name="logPath" source="log.path" defaultValue="${user.dir}/logs"/>

    <appender name="FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${logPath}/log-info.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${logPath}/history-log-%d{yyyy-MM-dd}.zip</FileNamePattern>
            <maxHistory>5</maxHistory>
        </rollingPolicy>
        <encoder>
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</Pattern>
        </encoder>
    </appender>

    <appender name="FILE_APPENDER_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>12000</queueSize>
        <neverBlock>true</neverBlock>
        <appender-ref ref="FILE_APPENDER"/>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE_APPENDER_ASYNC"/>

        <springProfile name="!prod">
            <appender-ref ref="CONSOLE"/>
        </springProfile>
    </root>

    <logger name="com.example" level="INFO"/>
</configuration>

备注

Spring Boot工程中建议将logback文件命名为logback-spring.xml

When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-logging

Views: 10,793 · Posted: 2020-12-25

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh