Polish "Use Console charset for console logging when available"

See gh-44353
This commit is contained in:
Moritz Halbritter
2025-02-25 14:52:13 +01:00
parent a250bbb7e4
commit b2d7a13b9d
2 changed files with 37 additions and 8 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.logging;
import java.io.Console;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.BiConsumer;
import java.util.function.Function;
@@ -95,10 +96,6 @@ public class LoggingSystemProperties {
return System.console();
}
protected Charset getDefaultCharset() {
return Charset.defaultCharset();
}
public final void apply() {
apply(null);
}
@@ -120,11 +117,14 @@ public class LoggingSystemProperties {
}
protected void apply(LogFile logFile, PropertyResolver resolver) {
Charset defaultCharset = getDefaultCharset();
Charset consoleCharset = (defaultCharset != null) ? defaultCharset : getDefaultConsoleCharset();
Charset fileCharset = (defaultCharset != null) ? defaultCharset : getDefaultFileCharset();
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME, resolver);
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP, resolver);
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, getConsoleCharset().name());
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, getDefaultCharset().name());
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, consoleCharset.name());
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, fileCharset.name());
setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper);
setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper);
setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver);
@@ -140,9 +140,32 @@ public class LoggingSystemProperties {
}
}
private Charset getConsoleCharset() {
/**
* Returns the default charset.
* @return the default charset
* @deprecated since 3.5.0 for removal in 3.7.0 in favor of
* {@link #getDefaultConsoleCharset()} and {@link #getDefaultFileCharset()}.
*/
@Deprecated(since = "3.5.0", forRemoval = true)
protected Charset getDefaultCharset() {
return null;
}
/**
* Returns the default console charset.
* @return returns the default console charset
*/
protected Charset getDefaultConsoleCharset() {
Console console = getConsole();
return (console != null) ? console.charset() : getDefaultCharset();
return (console != null) ? console.charset() : Charset.defaultCharset();
}
/**
* Returns the default file charset.
* @return returns the default file charset
*/
protected Charset getDefaultFileCharset() {
return StandardCharsets.UTF_8;
}
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.logging.logback;
import java.io.Console;
import java.nio.charset.Charset;
import java.util.function.BiConsumer;
import java.util.function.Function;
@@ -75,6 +76,11 @@ public class LogbackLoggingSystemProperties extends LoggingSystemProperties {
return super.getConsole();
}
@Override
protected Charset getDefaultFileCharset() {
return Charset.defaultCharset();
}
@Override
protected void apply(LogFile logFile, PropertyResolver resolver) {
super.apply(logFile, resolver);