趁今天有时间整理了一下
启动命令为
//开发环境
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" /><span style="color: #0000ff"><</span><span style="color: #800000">appender </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="console"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ch.qos.logback.core.ConsoleAppender"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">filter </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="ch.qos.logback.classic.filter.ThresholdFilter"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">level</span><span style="color: #0000ff">></span>debug<span style="color: #0000ff"></</span><span style="color: #800000">level</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span><span style="color: #000000">%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n </span><span style="color: #0000ff"></</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">appender</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">appender </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="file"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ch.qos.logback.core.rolling.RollingFileAppender"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">file</span><span style="color: #0000ff">></span>${log.path}<span style="color: #0000ff"></</span><span style="color: #800000">file</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">rollingPolicy </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">fileNamePattern</span><span style="color: #0000ff">></span>${log.path}.%d{yyyy-MM-dd}.zip<span style="color: #0000ff"></</span><span style="color: #800000">fileNamePattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">rollingPolicy</span><span style="color: #0000ff">></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span><span style="color: #000000">%date %level [%thread] %logger{36} [%file : %line] %msg%n </span><span style="color: #0000ff"></</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">appender</span><span style="color: #0000ff">></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">root </span><span style="color: #ff0000">level</span><span style="color: #0000ff">="warn"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">appender-ref </span><span style="color: #ff0000">ref</span><span style="color: #0000ff">="console"</span> <span style="color: #0000ff">/></span></br> <span style="color: #008000"><!--</span><span style="color: #008000"><appender-ref ref="file" /></span><span style="color: #008000">--></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">root</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.springframework.scheduling"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.catalina.util.LifecycleBase"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.coyote.http11.Http11NioProtocol"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.tomcat.util.net.NioSelectorPool"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.springframework"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="debug"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="com.lzw"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="debug"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.mybatis"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="debug"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.Connection"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.Statement"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.PreparedStatement"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.ResultSet"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br>
</configuration>
logback-qa.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration debug="true"> <contextName>logback</contextName><span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="log.path"</span><span style="color: #ff0000"> value</span><span style="color: #0000ff">="lzw/logback.log"</span> <span style="color: #0000ff">/></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">appender </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="console"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ch.qos.logback.core.ConsoleAppender"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">filter </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="ch.qos.logback.classic.filter.ThresholdFilter"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">level</span><span style="color: #0000ff">></span>debug<span style="color: #0000ff"></</span><span style="color: #800000">level</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">filter</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span><span style="color: #000000">%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n </span><span style="color: #0000ff"></</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">appender</span><span style="color: #0000ff">></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">appender </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="file"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="ch.qos.logback.core.rolling.RollingFileAppender"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">file</span><span style="color: #0000ff">></span>${log.path}<span style="color: #0000ff"></</span><span style="color: #800000">file</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">rollingPolicy </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">fileNamePattern</span><span style="color: #0000ff">></span>${log.path}.%d{yyyy-MM-dd}.zip<span style="color: #0000ff"></</span><span style="color: #800000">fileNamePattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">rollingPolicy</span><span style="color: #0000ff">></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span><span style="color: #000000">%date %level [%thread] %logger{36} [%file : %line] %msg%n </span><span style="color: #0000ff"></</span><span style="color: #800000">pattern</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">encoder</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">appender</span><span style="color: #0000ff">></span></br></br> <span style="color: #0000ff"><</span><span style="color: #800000">root </span><span style="color: #ff0000">level</span><span style="color: #0000ff">="warn"</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">appender-ref </span><span style="color: #ff0000">ref</span><span style="color: #0000ff">="file"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"></</span><span style="color: #800000">root</span><span style="color: #0000ff">></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.springframework.scheduling"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="com.lzw"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.catalina.util.LifecycleBase"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.coyote.http11.Http11NioProtocol"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.apache.tomcat.util.net.NioSelectorPool"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">Logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.springframework"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="org.mybatis"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="warn"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.Connection"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.Statement"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.PreparedStatement"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span> <span style="color: #0000ff"><</span><span style="color: #800000">logger </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="java.sql.ResultSet"</span><span style="color: #ff0000"> level</span><span style="color: #0000ff">="error"</span> <span style="color: #0000ff">/></span></br>
</configuration>