Skip to content

Commit b2d7a13

Browse files
committed
Polish "Use Console charset for console logging when available"
See gh-44353
1 parent a250bbb commit b2d7a13

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.Console;
2020
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2122
import java.util.function.BiConsumer;
2223
import java.util.function.Function;
2324

@@ -95,10 +96,6 @@ protected Console getConsole() {
9596
return System.console();
9697
}
9798

98-
protected Charset getDefaultCharset() {
99-
return Charset.defaultCharset();
100-
}
101-
10299
public final void apply() {
103100
apply(null);
104101
}
@@ -120,11 +117,14 @@ private PropertyResolver getPropertyResolver() {
120117
}
121118

122119
protected void apply(LogFile logFile, PropertyResolver resolver) {
120+
Charset defaultCharset = getDefaultCharset();
121+
Charset consoleCharset = (defaultCharset != null) ? defaultCharset : getDefaultConsoleCharset();
122+
Charset fileCharset = (defaultCharset != null) ? defaultCharset : getDefaultFileCharset();
123123
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME, resolver);
124124
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP, resolver);
125125
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
126-
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, getConsoleCharset().name());
127-
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, getDefaultCharset().name());
126+
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, consoleCharset.name());
127+
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, fileCharset.name());
128128
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper);
129129
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper);
130130
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
@@ -140,9 +140,32 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
140140
}
141141
}
142142

143-
private Charset getConsoleCharset() {
143+
/**
144+
* Returns the default charset.
145+
* @return the default charset
146+
* @deprecated since 3.5.0 for removal in 3.7.0 in favor of
147+
* {@link #getDefaultConsoleCharset()} and {@link #getDefaultFileCharset()}.
148+
*/
149+
@Deprecated(since = "3.5.0", forRemoval = true)
150+
protected Charset getDefaultCharset() {
151+
return null;
152+
}
153+
154+
/**
155+
* Returns the default console charset.
156+
* @return returns the default console charset
157+
*/
158+
protected Charset getDefaultConsoleCharset() {
144159
Console console = getConsole();
145-
return (console != null) ? console.charset() : getDefaultCharset();
160+
return (console != null) ? console.charset() : Charset.defaultCharset();
161+
}
162+
163+
/**
164+
* Returns the default file charset.
165+
* @return returns the default file charset
166+
*/
167+
protected Charset getDefaultFileCharset() {
168+
return StandardCharsets.UTF_8;
146169
}
147170

148171
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.logging.logback;
1818

1919
import java.io.Console;
20+
import java.nio.charset.Charset;
2021
import java.util.function.BiConsumer;
2122
import java.util.function.Function;
2223

@@ -75,6 +76,11 @@ protected Console getConsole() {
7576
return super.getConsole();
7677
}
7778

79+
@Override
80+
protected Charset getDefaultFileCharset() {
81+
return Charset.defaultCharset();
82+
}
83+
7884
@Override
7985
protected void apply(LogFile logFile, PropertyResolver resolver) {
8086
super.apply(logFile, resolver);

0 commit comments

Comments
 (0)