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:
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:
Then, start your Spring Boot application with the command:
Check again
After applying the above configuration, check the log file again. The characters should now be displayed correctly:
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!