美文网首页程序员
SpringBoot 不同环境,加载不同日志配置

SpringBoot 不同环境,加载不同日志配置

作者: 凌云_00 | 来源:发表于2020-06-07 17:51 被阅读0次

开篇

  忽然想起好久不写博客,今天就搞一个

背景

  现有项目集成的日志采集框架有点小问题,每次运行时,都会在日志加载这里卡顿半分钟,非常影响工作效率.并且本地单测或者运行,也没有日志采集的必要性,所以我们今天改造项目,实现不同环境下加载不同的日志配置,这样在开发环境和测试环境下不再启动日志采集的配置,加快项目启动

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 加载不同日志配置),实现不同环境下加载不同的日志配置

相关文章

网友评论

    本文标题:SpringBoot 不同环境,加载不同日志配置

    本文链接:https://www.haomeiwen.com/subject/nhwctktx.html