springboot-How to solve springboot log file encoding problem?

How to solve springboot encoding problem when collecting logs of springboot apps ?

Problem

When you want to use springboot, you would get this problem ( The log files contains not-properly-displayed characters for double-byte languages):


2020-09-01 16:24:19.467  INFO 4912 --- [container-6] '?????????1298?09?01?'

Environment

  • SpringBoot 1.2.5
  • JDK 1.8

Solution

You should set the log file encoding of springboot by adding this configurations to your command line:

-Dfile.encoding=UTF-8

Then start your springboot application in the command line:

/opt/jdk1.8.0_40/bin/java -Dfile.encoding=UTF-8 -Xms256m -Xmx1024m -Djava.library.path=./lib -classpath .... com.bswen.Application

Check again

Then you can check the logs file again:

2020-09-01 16:24:19.467  INFO 4912 --- [container-6] '正确09年01日'

Theory about this

According to this article:

Default Character encoding in Java or charset is the character encoding used by JVM to convert bytes into Strings or characters when you don’t define java system property “file.encoding”. Java gets character encoding by calling System.getProperty(“file.encoding”,”UTF-8”) at the time of JVM start-up. So if Java doesn’t get any file.encoding attribute it uses “UTF-8” character encoding for all practical purpose e.g. on String.getBytes() or Charset.defaultCharSet().