"Spring boot Logback" Logback log garbled problem in spring boot

Source: Internet
Author: User

After the initial use of the Logback custom configuration file to control the spring boot log completely, an intolerable problem was found, that is, the log garbled.

The console sees the print log garbled as follows:

and the log file opens:

Notepad opens

Sublime Open

On this issue, the analysis is as follows:

1. Printed "Enter Controller log control center----" "", it can be seen that at least the code itself and the IDE environment encoding method does not have any relationship;

2. The printed log is displayed in the console after the Logback-spring.xml file has worked, and when the log file is opened, it is not the same to see a different text editor open.

Conclusion:

1. To be sure, this is the result of the Logback-spring.xml custom configuration

2. The log file is not garbled, but different editing software is the default encoding method is not the same. So there will be garbled, and some are not garbled.

Observe the Logback-spring.xml file. can see:

Workaround:

So, we just need to set the value in <charset> to the encoding we want.

Basic Complete Logback-spring.xml File code: "Note that the charset of the file section is still GBK"

 

<?xml version= "1.0" encoding= "UTF-8"?>
<configuration>
<!--file output format--
<property name= "PATTERN" value= "%-12 (%d{yyyy-mm-dd HH:mm:ss. SSS}) |-%-5level [%thread]%c [%l]-| %msg%n "/>
<!--Dev--
<property name= "Dev_file_path" value= "D:/datarun.log"/>
<!--test file path--
<property name= "Test_file_path" value= "D:/test.log"/>
<!--Pro file path--
<property name= "Pro_file_path" value= "/opt/test/log"/>

<!--development Environment--
<springprofile name= "Dev" >
<appender name= "CONSOLE" class= "Ch.qos.logback.core.ConsoleAppender" >
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>

</appender>
<!--produce one file per day--
<appender name= "Dev-file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<!--file path--
<file>${DEV_FILE_PATH}</file>
<encoder>
<pattern>${PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<!--file name--
<fileNamePattern>${DEV_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--file Maximum saved history-
<MaxHistory>100</MaxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>${PATTERN}</pattern>
</layout>
</appender>
<logger name= "Org.springframework" level= "Debug"/>
<logger name= "java.sql" level= "Debug"/>
<logger name= "org" level= "Debug"/>
<logger name= "Com.guige" level= "Debug"/>
<root level= "Debug" >
<appender-ref ref= "CONSOLE"/>
<appender-ref ref= "Dev-file"/>
</root>
</springProfile>

<!--test Environment--
<springprofile name= "Test" >
<!--produce one file per day--
<appender name= "Test-file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<!--file path--
<file>${TEST_FILE_PATH}</file>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<!--file name--
<fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!--file Maximum saved history-
<MaxHistory>100</MaxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>${PATTERN}</pattern>
</layout>
</appender>
<root level= "Info" >
<appender-ref ref= "Test-file"/>
</root>
</springProfile>

<!--production Environment--
<springprofile name= "prod" >
<appender name= "Prod_file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<file>${PRO_FILE_PATH}</file>
<rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>100</MaxHistory>
</rollingPolicy>
<layout class= "Ch.qos.logback.classic.PatternLayout" >
<pattern>${PATTERN}</pattern>
</layout>
</appender>
<root level= "Warn" >
<appender-ref ref= "Prod_file"/>
</root>
</springProfile>
</configuration>

Operation Result:

"Spring boot Logback" Logback log garbled problem in spring boot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.