SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

Posted 高因咖啡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active相关的知识,希望对你有一定的参考价值。

趁今天有时间整理了一下

 

启动命令为

//开发环境

java -jar app.jar --spring.profiles.active=dev--server.port=8060

//测试环境

java -jar app.jar --spring.profiles.active=qa --server.port=8060

//生产环境

java -jar app.jar --spring.profiles.active=prod --server.port=8060

 

废话少说,直接上代码

application.yml,打包为jar的时候主要把spring.profile.active注释掉.否则 命令行参数 --spring.profiles.active可能不生效,不知何解

spring:
  profiles:
    #指定读取配置文件:dev(开发环境),prod(生产环境),qa(测试环境)
    active: qa

#日志
logging:
  config: classpath:logback-${spring.profiles.active}.xml

 logback-dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <contextName>logback</contextName>
    <property name="log.path" value="lzw/logback.log" />

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
        </rollingPolicy>

        <encoder>
            <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="warn">
        <appender-ref ref="console" />
        <!--<appender-ref ref="file" />-->
    </root>
    <logger name="org.springframework.scheduling"  level="error" />
    <Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
    <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
    <Logger name="org.apache.tomcat.util.net.NioselectorPool" level="warn" />
    <Logger name="org.springframework" level="debug" />
    <logger name="com.lzw" level="debug" />
    <logger name="org.mybatis" level="debug" />
    <logger name="java.sql" level="error" />
    <logger name="java.sql.Connection" level="error" />
    <logger name="java.sql.Statement" level="error" />
    <logger name="java.sql.PreparedStatement" level="error" />
    <logger name="java.sql.ResultSet" level="error" />
</configuration>

logback-qa.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration debug="true">
    <contextName>logback</contextName>

    <property name="log.path" value="lzw/logback.log" />

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debug</level>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
        </rollingPolicy>

        <encoder>
            <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="warn">
        <appender-ref ref="file" />
    </root>
    <logger name="org.springframework.scheduling"  level="error" />
    <logger name="com.lzw" level="warn" />
    <Logger name="org.apache.catalina.util.LifecycleBase" level="warn" />
    <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
    <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
    <Logger name="org.springframework" level="warn" />
    <logger name="org.mybatis" level="warn" />
    <logger name="java.sql" level="error" />
    <logger name="java.sql.Connection" level="error" />
    <logger name="java.sql.Statement" level="error" />
    <logger name="java.sql.PreparedStatement" level="error" />
    <logger name="java.sql.ResultSet" level="error" />
</configuration>

 

以上是关于SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active的主要内容,如果未能解决你的问题,请参考以下文章

使用druid的监控组件进行数据库连接池的监控

Grails 3 - 更改运行应用程序的默认端口

Spring Cloud Gateway 中每个备用请求的 404 错误

Logback - 你能从 env 变量中定义 appender 名称和类吗?

SpringBoot入门到精通-SpringBoot自动配置原理

SpringBoot入门到精通-SpringBoot启动流程