Skip to content

Commit

Permalink
Merge pull request #44353 from nosan
Browse files Browse the repository at this point in the history
* pr/44353:
  Polish "Use Console charset for console logging when available"
  Use Console charset for console logging when available

Closes gh-44353
  • Loading branch information
mhalbritter committed Feb 28, 2025
2 parents 9364879 + b2d7a13 commit 278c37d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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;
Expand Down Expand Up @@ -91,8 +92,8 @@ public LoggingSystemProperties(Environment environment, Function<String, String>
this.setter = (setter != null) ? setter : systemPropertySetter;
}

protected Charset getDefaultCharset() {
return StandardCharsets.UTF_8;
protected Console getConsole() {
return System.console();
}

public final void apply() {
Expand All @@ -116,12 +117,14 @@ private PropertyResolver getPropertyResolver() {
}

protected void apply(LogFile logFile, PropertyResolver resolver) {
String defaultCharsetName = getDefaultCharset().name();
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, defaultCharsetName);
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
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);
Expand All @@ -137,6 +140,34 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
}
}

/**
* 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() : 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) {
setSystemProperty(property, resolver, Function.identity());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,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;
Expand Down Expand Up @@ -71,7 +72,12 @@ public LogbackLoggingSystemProperties(Environment environment, Function<String,
}

@Override
protected Charset getDefaultCharset() {
protected Console getConsole() {
return super.getConsole();
}

@Override
protected Charset getDefaultFileCharset() {
return Charset.defaultCharset();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,6 +16,9 @@

package org.springframework.boot.logging;

import java.io.Console;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -31,6 +34,9 @@
import org.springframework.mock.env.MockEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

/**
* Tests for {@link LoggingSystemProperties}.
Expand Down Expand Up @@ -72,9 +78,22 @@ void consoleLogPatternIsSet() {
}

@Test
void consoleCharsetWhenNoPropertyUsesUtf8() {
new LoggingSystemProperties(new MockEnvironment()).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo("UTF-8");
void consoleCharsetWhenNoPropertyUsesCharsetDefault() {
LoggingSystemProperties loggingSystemProperties = spy(new LoggingSystemProperties(new MockEnvironment()));
given(loggingSystemProperties.getConsole()).willReturn(null);
loggingSystemProperties.apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET)).isEqualTo(Charset.defaultCharset().name());
}

@Test
void consoleCharsetWhenNoPropertyUsesSystemConsoleCharsetWhenAvailable() {
LoggingSystemProperties loggingSystemProperties = spy(new LoggingSystemProperties(new MockEnvironment()));
Console console = mock(Console.class);
given(console.charset()).willReturn(StandardCharsets.UTF_16BE);
given(loggingSystemProperties.getConsole()).willReturn(console);
loggingSystemProperties.apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET))
.isEqualTo(StandardCharsets.UTF_16BE.name());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 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.
Expand All @@ -16,7 +16,9 @@

package org.springframework.boot.logging.logback;

import java.io.Console;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

/**
* Tests for {@link LogbackLoggingSystemProperties}.
Expand Down Expand Up @@ -102,11 +107,29 @@ void applySetsLogbackSystemPropertiesFromDeprecated() {

@Test
void consoleCharsetWhenNoPropertyUsesDefault() {
new LoggingSystemProperties(new MockEnvironment()).apply(null);
LogbackLoggingSystemProperties logbackLoggingSystemProperties = spy(
new LogbackLoggingSystemProperties(new MockEnvironment(), null, null));
given(logbackLoggingSystemProperties.getConsole()).willReturn(null);

logbackLoggingSystemProperties.apply(null);
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName()))
.isEqualTo(Charset.defaultCharset().name());
}

@Test
void consoleCharsetWhenNoPropertyUsesSystemConsoleCharsetWhenAvailable() {
LogbackLoggingSystemProperties logbackLoggingSystemProperties = spy(
new LogbackLoggingSystemProperties(new MockEnvironment(), null, null));

Console console = mock(Console.class);
given(console.charset()).willReturn(StandardCharsets.UTF_16BE);
given(logbackLoggingSystemProperties.getConsole()).willReturn(console);

logbackLoggingSystemProperties.apply(null);
assertThat(System.getProperty(LoggingSystemProperty.CONSOLE_CHARSET.getEnvironmentVariableName()))
.isEqualTo(StandardCharsets.UTF_16BE.name());
}

@Test
void fileCharsetWhenNoPropertyUsesDefault() {
new LoggingSystemProperties(new MockEnvironment()).apply(null);
Expand Down

0 comments on commit 278c37d

Please sign in to comment.