Skip to content

Commit 278c37d

Browse files
committed
Merge pull request #44353 from nosan
* pr/44353: Polish "Use Console charset for console logging when available" Use Console charset for console logging when available Closes gh-44353
2 parents 9364879 + b2d7a13 commit 278c37d

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
lines changed

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging;
1818

19+
import java.io.Console;
1920
import java.nio.charset.Charset;
2021
import java.nio.charset.StandardCharsets;
2122
import java.util.function.BiConsumer;
@@ -91,8 +92,8 @@ public LoggingSystemProperties(Environment environment, Function<String, String>
9192
this.setter = (setter != null) ? setter : systemPropertySetter;
9293
}
9394

94-
protected Charset getDefaultCharset() {
95-
return StandardCharsets.UTF_8;
95+
protected Console getConsole() {
96+
return System.console();
9697
}
9798

9899
public final void apply() {
@@ -116,12 +117,14 @@ private PropertyResolver getPropertyResolver() {
116117
}
117118

118119
protected void apply(LogFile logFile, PropertyResolver resolver) {
119-
String defaultCharsetName = getDefaultCharset().name();
120+
Charset defaultCharset = getDefaultCharset();
121+
Charset consoleCharset = (defaultCharset != null) ? defaultCharset : getDefaultConsoleCharset();
122+
Charset fileCharset = (defaultCharset != null) ? defaultCharset : getDefaultFileCharset();
120123
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME, resolver);
121124
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP, resolver);
122125
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
123-
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
124-
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
126+
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, consoleCharset.name());
127+
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, fileCharset.name());
125128
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper);
126129
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper);
127130
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
@@ -137,6 +140,34 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
137140
}
138141
}
139142

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() {
159+
Console console = getConsole();
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;
169+
}
170+
140171
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
141172
setSystemProperty(property, resolver, Function.identity());
142173
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging.logback;
1818

19+
import java.io.Console;
1920
import java.nio.charset.Charset;
2021
import java.util.function.BiConsumer;
2122
import java.util.function.Function;
@@ -71,7 +72,12 @@ public LogbackLoggingSystemProperties(Environment environment, Function<String,
7172
}
7273

7374
@Override
74-
protected Charset getDefaultCharset() {
75+
protected Console getConsole() {
76+
return super.getConsole();
77+
}
78+
79+
@Override
80+
protected Charset getDefaultFileCharset() {
7581
return Charset.defaultCharset();
7682
}
7783

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.logging;
1818

19+
import java.io.Console;
20+
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
1922
import java.util.Collections;
2023
import java.util.HashSet;
2124
import java.util.Map;
@@ -31,6 +34,9 @@
3134
import org.springframework.mock.env.MockEnvironment;
3235

3336
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.spy;
3440

3541
/**
3642
* Tests for {@link LoggingSystemProperties}.
@@ -72,9 +78,22 @@ void consoleLogPatternIsSet() {
7278
}
7379

7480
@Test
75-
void consoleCharsetWhenNoPropertyUsesUtf8() {
76-
new LoggingSystemProperties(new MockEnvironment()).apply(null);
77-
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo("UTF-8");
81+
void consoleCharsetWhenNoPropertyUsesCharsetDefault() {
82+
LoggingSystemProperties loggingSystemProperties = spy(new LoggingSystemProperties(new MockEnvironment()));
83+
given(loggingSystemProperties.getConsole()).willReturn(null);
84+
loggingSystemProperties.apply(null);
85+
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo(Charset.defaultCharset().name());
86+
}
87+
88+
@Test
89+
void consoleCharsetWhenNoPropertyUsesSystemConsoleCharsetWhenAvailable() {
90+
LoggingSystemProperties loggingSystemProperties = spy(new LoggingSystemProperties(new MockEnvironment()));
91+
Console console = mock(Console.class);
92+
given(console.charset()).willReturn(StandardCharsets.UTF_16BE);
93+
given(loggingSystemProperties.getConsole()).willReturn(console);
94+
loggingSystemProperties.apply(null);
95+
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET))
96+
.isEqualTo(StandardCharsets.UTF_16BE.name());
7897
}
7998

8099
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemPropertiesTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.boot.logging.logback;
1818

19+
import java.io.Console;
1920
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2022
import java.util.HashSet;
2123
import java.util.Set;
2224

@@ -31,6 +33,9 @@
3133
import org.springframework.mock.env.MockEnvironment;
3234

3335
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.mockito.BDDMockito.given;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.spy;
3439

3540
/**
3641
* Tests for {@link LogbackLoggingSystemProperties}.
@@ -102,11 +107,29 @@ void applySetsLogbackSystemPropertiesFromDeprecated() {
102107

103108
@Test
104109
void consoleCharsetWhenNoPropertyUsesDefault() {
105-
new LoggingSystemProperties(new MockEnvironment()).apply(null);
110+
LogbackLoggingSystemProperties logbackLoggingSystemProperties = spy(
111+
new LogbackLoggingSystemProperties(new MockEnvironment(), null, null));
112+
given(logbackLoggingSystemProperties.getConsole()).willReturn(null);
113+
114+
logbackLoggingSystemProperties.apply(null);
106115
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName()))
107116
.isEqualTo(Charset.defaultCharset().name());
108117
}
109118

119+
@Test
120+
void consoleCharsetWhenNoPropertyUsesSystemConsoleCharsetWhenAvailable() {
121+
LogbackLoggingSystemProperties logbackLoggingSystemProperties = spy(
122+
new LogbackLoggingSystemProperties(new MockEnvironment(), null, null));
123+
124+
Console console = mock(Console.class);
125+
given(console.charset()).willReturn(StandardCharsets.UTF_16BE);
126+
given(logbackLoggingSystemProperties.getConsole()).willReturn(console);
127+
128+
logbackLoggingSystemProperties.apply(null);
129+
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName()))
130+
.isEqualTo(StandardCharsets.UTF_16BE.name());
131+
}
132+
110133
@Test
111134
void fileCharsetWhenNoPropertyUsesDefault() {
112135
new LoggingSystemProperties(new MockEnvironment()).apply(null);

0 commit comments

Comments
 (0)