diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 385573a43a6..8d3417fee26 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -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) { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java index dad314206a2..001361f7449 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java @@ -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);