Skip to content

Commit e4e1958

Browse files
committed
Make Logback fall back to JVM's default charset
The charset "default" is an alias for US-ASCII, not the JVM's default charset. This commit updates the built-in Logback configuration to use Charset.defaultCharset().name() in place of "default" in the Java-based configuration. In the XML-based configuration where Charset.defaultCharset().name() cannot be called, we emulate its behaviour [1] by using the file.encoding system property, falling back to UTF-8 when it's not set. Fixes gh-27230 [1] https://github.com/openjdk/jdk8u/blob/19be6113dd0f658f950583b751284961d8ce0458/jdk/src/share/classes/java/nio/charset/Charset.java#L604-L617
1 parent 22d85e6 commit e4e1958

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,11 +72,14 @@ private void defaults(LogbackConfigurator config) {
7272
+ "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) "
7373
+ "%clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} "
7474
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"));
75-
config.getContext().putProperty("CONSOLE_LOG_CHARSET", resolve(config, "${CONSOLE_LOG_CHARSET:-default}"));
75+
String defaultCharset = Charset.defaultCharset().name();
76+
config.getContext().putProperty("CONSOLE_LOG_CHARSET",
77+
resolve(config, "${CONSOLE_LOG_CHARSET:-" + defaultCharset + "}"));
7678
config.getContext().putProperty("FILE_LOG_PATTERN", resolve(config, "${FILE_LOG_PATTERN:-"
7779
+ "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] "
7880
+ "%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"));
79-
config.getContext().putProperty("FILE_LOG_CHARSET", resolve(config, "${FILE_LOG_CHARSET:-default}"));
81+
config.getContext().putProperty("FILE_LOG_CHARSET",
82+
resolve(config, "${FILE_LOG_CHARSET:-" + defaultCharset + "}"));
8083
config.logger("org.apache.catalina.startup.DigesterFactory", Level.ERROR);
8184
config.logger("org.apache.catalina.util.LifecycleBase", Level.ERROR);
8285
config.logger("org.apache.coyote.http11.Http11NioProtocol", Level.WARN);

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Default logback configuration provided for import
1010
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
1111

1212
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
13-
<property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-default}"/>
13+
<property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/>
1414
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
15-
<property name="FILE_LOG_CHARSET" value="${FILE_LOG_CHARSET:-default}"/>
15+
<property name="FILE_LOG_CHARSET" value="${FILE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/>
1616

1717
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
1818
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>

0 commit comments

Comments
 (0)