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 89092d9c8a92..385573a43a6f 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 @@ -16,8 +16,8 @@ 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; @@ -91,8 +91,12 @@ public LoggingSystemProperties(Environment environment, Function this.setter = (setter != null) ? setter : systemPropertySetter; } + protected Console getConsole() { + return System.console(); + } + protected Charset getDefaultCharset() { - return StandardCharsets.UTF_8; + return Charset.defaultCharset(); } public final void apply() { @@ -116,12 +120,11 @@ private PropertyResolver getPropertyResolver() { } protected void apply(LogFile logFile, PropertyResolver resolver) { - String defaultCharsetName = getDefaultCharset().name(); setSystemProperty(LoggingSystemProperty.APPLICATION_NAME, resolver); setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP, resolver); setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString()); - setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName); - setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName); + setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, getConsoleCharset().name()); + setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, getDefaultCharset().name()); setSystemProperty(LoggingSystemProperty.CONSOLE_THRESHOLD, resolver, this::thresholdMapper); setSystemProperty(LoggingSystemProperty.FILE_THRESHOLD, resolver, this::thresholdMapper); setSystemProperty(LoggingSystemProperty.EXCEPTION_CONVERSION_WORD, resolver); @@ -137,6 +140,11 @@ protected void apply(LogFile logFile, PropertyResolver resolver) { } } + private Charset getConsoleCharset() { + Console console = getConsole(); + return (console != null) ? console.charset() : getDefaultCharset(); + } + private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) { setSystemProperty(property, resolver, Function.identity()); } 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 75ab70a9d2b9..dad314206a2e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package org.springframework.boot.logging.logback; -import java.nio.charset.Charset; +import java.io.Console; import java.util.function.BiConsumer; import java.util.function.Function; @@ -71,8 +71,8 @@ public LogbackLoggingSystemProperties(Environment environment, Function