軟件生存周期中,涉及代碼運行的環節有編碼、測試和維護階段,而一套成熟的代碼,在此三個階段,數據庫、日志路徑、日志級別、線程池大小等配置一般會不一樣。作為開發人員,希望將代碼與配置解耦合,不同的環境,代碼一套,而配置多套。
針對于多環境的配置,可以使用maven的profile及filter配置,在打包環節通過打包命令 mvn clean package -p dev/test/product決定所打環境的war/jar包。此種解決方案,產生的war\jar包在不同環境的是不同的,因此md5校驗和也不同。一次敏捷開發結束后,開發、測試、線上的的war/jar包,只能人為添加標識來識別,比如test-1.0.1和prod-1.0.1是功能相同、環境不同的war/jar包。如果是spring boot項目,可以使用yaml配置,實現多環境配置,在項目啟動時,通過添加參數--spring.profiles.active=dev/test/production,指定項目運行的環境。此方案的jar包在不同運行環境均是一個,不會出現測試與生產的war/jar包代碼不一致的問題(第一種方案在測試打包后,生產打包前,可能會有代碼提交,需人工控制此階段的行為)。
本文基于第二種配置方案,但在使用logback作為日志方案時,產生了一些問題, 具體見下文。
問題1:
使用application.yml配置多環境變量,使用logback.xml實現日志配置,不能實現多環境配置(即logback配置未生效),打印的日志路徑和日志級別不是配置文件中的值。
項目配置文件-application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
spring: profiles.active: dev --- spring: profiles: dev log: path: ./logs level: debug --- spring: profiles: test log: path: /home/user/logs/ level: info --- |
日志配置文件-logback.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration debug= "true" scan= "true" scanperiod= "30 seconds" > <appender name= "stdout" > <encoder> <pattern>%d{yyyy-mm-dd hh:mm:ss} [%level] [% class :%line] - %m %n</pattern> </encoder> </appender> <appender name= "file-out" > <file>${log.path}/xxx.log</file> <encoder> <pattern>%d{yyyy-mm-dd hh:mm:ss} [%level] [% class :%line] - %m %n</pattern> </encoder> <rollingpolicy> <filenamepattern>${log.path}/xxx.%d{yyyy-mm-dd}.log.zip</filenamepattern> <!-- 30 days --> <maxhistory> 30 </maxhistory> </rollingpolicy> </appender> <root level= "${log.level}" > <appender-ref ref= "stdout" /> <appender-ref ref= "file-out" /> </root> </configuration> |
查閱官方文檔( http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-custom-log-levels),發現問題之所在
即,logback.xml加載早于application.yml,需改用logback-spring.xml實現日志配置
問題2:
經上修改后,發現配置文件已生效,但logback-spring.xml中的變量并未生效,日志內容見下
1
2
3
4
5
6
|
11 : 41 : 11 , 450 |-info in c.q.l.core.rolling.timebasedrollingpolicy @962287291 - will use the pattern log.path_is_undefined/error.%d{yyyy-mm-dd}.log for the active file 11 : 41 : 11 , 453 |-info in c.q.l.core.rolling.defaulttimebasedfilenamingandtriggeringpolicy - the date pattern is 'yyyy-mm-dd' from file name pattern 'log.path_is_undefined/error.%d{yyyy-mm-dd}.log.zip' . ... 11 : 41 : 11 , 471 |-info in ch.qos.logback.classic.joran.action.rootloggeraction - setting level of root logger to debug |
看似log.level已生效,log.path未生效,其實不然,經修改application.yml中log.path: others(info, error),日志都為以上內容
查看官方文檔
官方文檔指明,需要使用<springproperty>,才可使用application.properties(或application.yml)中的值
經修改logback-spring.xml后,問題解決
最終的日志配置文件-logback-spring.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version= "1.0" encoding= "utf-8" ?> <configuration debug= "true" scan= "true" scanperiod= "30 seconds" > <springproperty scope= "context" name= "loglevel" source= "log.level" /> <springproperty scope= "context" name= "logpath" source= "log.path" /> <appender name= "stdout" > <encoder> <pattern>%d{yyyy-mm-dd hh:mm:ss} [%level] [% class :%line] - %m %n</pattern> </encoder> </appender> <appender name= "file-out" > <file>${logpath}/xxx.log</file> <encoder> <pattern>%d{yyyy-mm-dd hh:mm:ss} [%level] [% class :%line] - %m %n</pattern> </encoder> <rollingpolicy> <filenamepattern>${logpath}/xxx.%d{yyyy-mm-dd}.log.zip</filenamepattern> <!-- 30 days --> <maxhistory> 30 </maxhistory> </rollingpolicy> </appender> <root level= "${loglevel}" > <appender-ref ref= "stdout" /> <appender-ref ref= "file-out" /> </root> </configuration> |
備注:
1.本文暫不討論使用配置中心實現多環境配置管理
2. how to package a maven program?
mvn clean package [-dmaven.test.skip]
3.how to start a spring boot program?
java -jar xxx-1.0.0.jar --spring.profiles.active=dev(default)/test/production [--log.level=debug]
其中,--log.level仍可以修改--spring.profiles.active生效后的變量值,可用于線上環境debug(不用重新打包,重新啟動即可),但是不建議線上debug。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/vitech/article/details/53812137