Skip to content

Console logging incorrectly uses Charset.defaultCharset() #43118

Closed as not planned
@crmky

Description

@crmky

JEP 400: UTF-8 by Default was included in Java 18 and later version. Basically it changes the default file.encoding to UTF-8 by default and introduced a few additional properties to identify system native encoding. However the console output charset is different:

Those APIs include the ones listed above, but not System.out and System.err, whose charset will be as specified by Console.charset().

However Spring Boot doesn't respect Console.charset(). It always use org.springframework.boot.logging.LoggingSystemProperties.getDefaultCharset() to set the default charset for console and file encoding. Depends on the implementation, it uses either hard-coded UTF-8 or Charset.defaultCharset().

Below is a simple Spring Boot application to reproduce the issue (The system locale needs to be different than English):

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("你好");
        log.info("你好");
    }

Run it with Java 17:

你好
2024-11-12T10:16:10.992+08:00  INFO 19176 --- [test] [           main] test.Application           : 你好

Run it with Java 21 (my system locale is GBK):

你好
2024-11-12T10:16:28.478+08:00  INFO 524 --- [test] [           main] test.Application           : 浣犲ソ

The workaround is to add properties like below. But I think a better value should be provided by default.

logging.charset.console=${stdout.encoding}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions