Skip to content

How to resolve springboot log file encoding problem?

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

Problem

When using Spring Boot, you might encounter an issue where log files contain improperly displayed characters, especially for double-byte languages. For example:

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

Environment

  • SpringBoot 1.2.5+
  • JDK 1.8+

Solution

To resolve this issue, you need to set the log file encoding for Spring Boot by adding the following configuration to your command line:

Terminal window
-Dfile.encoding=UTF-8

Then, start your Spring Boot application with the command:

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

Check again

After applying the above configuration, check the log file again. The characters should now be displayed correctly:

Terminal window
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().

Summary

In this post, we discussed how to resolve the Spring Boot log file encoding problem, particularly for double-byte languages. By setting the -Dfile.encoding=UTF-8 parameter in the command line, you can ensure that log files display characters correctly. This solution is essential for applications that handle multiple languages or require specific character encodings. Always verify your log files after applying the configuration to confirm that the issue is resolved.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!