SpringBoot 不同環境,加載不同日誌配置

開篇

  忽然想起好久不寫博客,今天就搞一個

背景

  現有項目集成的日誌採集框架有點小問題,每次運行時,都會在日誌加載這裏卡頓半分鐘,非常影響工作效率.並且本地單測或者運行,也沒有日誌採集的必要性,所以我們今天改造項目,實現不同環境下加載不同的日誌配置,這樣在開發環境和測試環境下不再啓動日誌採集的配置,加快項目啓動

Start

  項目 SpringBoot Version和Log Version

<properties>
  <spring-boot.version>2.1.8.RELEASE</spring-boot.version>
  <slf4j.version>1.7.14</slf4j.version>
  <logback.version>1.2.3</logback.version>
</properties>

One

  首先 maven 構建時要有一個profile(鑑於所有項目都有profile,我這裏貼一個簡單版本)

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>             
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>prev</id>
            <properties>
                <env>prev</env>
             </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

Two

  添加 maven resource 插件 和 build resource過程 (這裏必須要添加,保證maven 的 配置項 可以加載到 SpringBoot 中)

<build>
        <finalName>trade-service-inquire</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.ziroom.trade.rent.inquire.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- resource插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

Tree

  添加 SpringBoot 配置 (application.properties 文件中)

spring.profiles.active=@env@

Four

  改造原有的 logback-spring.xml 文件,使用 springProfile 包裹原有的日誌配置,並通過 name 指定不同環境下的日誌配置

    <springProfile name="test">
        <root level="INFO">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="allAppender"/>
        </root>
    </springProfile>

    <springProfile name="prod"> 
       <root level="DEBUG">
            <appender-ref ref="allAppender"/>
            <appender-ref ref="errorAppender"/>
       </root>
    </springProfile>

End

  整個改造的核心就是利用 maven 配置的profile 替換 springBoot 中的 profileActive,並利用 logback <springProfile>標籤所提供能力(根據spring.profile.active 加載不同日誌配置),實現不同環境下加載不同的日誌配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章