最近在做架構遷移工作,將原有springmvc項目調整至springboot架構上,遷移完後,發現用springboot以jar包形式啓動正常,用tomcat去啓動時日誌亂碼,但是項目部署至Linux環境啓動也正常,以下是日誌亂碼代碼:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE" />
<!-- 開發、測試環境 -->
<springProfile name="dev,test,devtest">
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO" />
<logger name="hwja.com" level="DEBUG" />
</springProfile>
<!-- 生產環境 -->
<springProfile name="prod">
<logger name="org.springframework.web" level="ERROR"/>
<logger name="org.springboot.sample" level="ERROR" />
<logger name="hwja.com" level="ERROR" />
</springProfile>
</configuration>
下面我們來分析一下:
windows系統默認編碼GBK,而項目編碼統一爲UTF-8,與springboot集成的logback日誌默認編碼也設置的是UTF-8,所以導致日誌輸出爲亂碼,Linux下面全爲UTF-8,所以正常,那麼如何解決呢,解決思路將springboot集成的logback日誌默認編碼去掉,讓它自動去讀操作系統的環境編碼,也就是默認的tomcat中的file.encoding的默認值,解決後的日誌代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<!--注意此處刪除編碼方式,讓其讀操作系統默認編碼-->