windows下启动tomcat,日志乱码问题,日志架构springboot+logback

最近在做架构迁移工作,将原有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>
<!--注意此处删除编码方式,让其读操作系统默认编码-->

如果仍然有问题,可以联系我QQ:604313052 WX:Ca604313052

猜你喜欢

转载自blog.51cto.com/11985461/2152467
今日推荐