Polish gh-39957 and gh-41444

See gh-42359
This commit is contained in:
Johnny Lim 2024-09-18 23:58:36 +09:00 committed by Stéphane Nicoll
parent e3aac5d442
commit 94e8c5db36
8 changed files with 21 additions and 24 deletions

View File

@ -712,13 +712,13 @@ The following listing shows three sample profiles:
If you want to refer to properties from your Spring `Environment` within your Log4j2 configuration you can use `spring:` prefixed https://logging.apache.org/log4j/2.x/manual/lookups.html[lookups]. If you want to refer to properties from your Spring `Environment` within your Log4j2 configuration you can use `spring:` prefixed https://logging.apache.org/log4j/2.x/manual/lookups.html[lookups].
Doing so can be useful if you want to access values from your `application.properties` file in your Log4j2 configuration. Doing so can be useful if you want to access values from your `application.properties` file in your Log4j2 configuration.
The following example shows how to set a Log4j2 property named `applicationName` and `applicationGroup` that reads `spring.application.name` and `spring.application.group` from the Spring `Environment`: The following example shows how to set Log4j2 properties named `applicationName` and `applicationGroup` that read `spring.application.name` and `spring.application.group` from the Spring `Environment`:
[source,xml] [source,xml]
---- ----
<Properties> <Properties>
<Property name="applicationName">${spring:spring.application.name}</Property> <Property name="applicationName">${spring:spring.application.name}</Property>
<Property name="applicationProperty">${spring:spring.application.property}</Property> <Property name="applicationGroup">${spring:spring.application.group}</Property>
</Properties> </Properties>
---- ----

View File

@ -32,6 +32,7 @@ public enum LoggingSystemProperty {
/** /**
* Logging system property for the application group that should be logged. * Logging system property for the application group that should be logged.
* @since 3.4.0
*/ */
APPLICATION_GROUP("APPLICATION_GROUP", "spring.application.group", "logging.include-application-group"), APPLICATION_GROUP("APPLICATION_GROUP", "spring.application.group", "logging.include-application-group"),

View File

@ -29,8 +29,8 @@ import org.apache.logging.log4j.core.pattern.PatternFormatter;
import org.apache.logging.log4j.core.pattern.PatternParser; import org.apache.logging.log4j.core.pattern.PatternParser;
/** /**
* Log4j2 {@link LogEventPatternConverter} used help format optional values that should be * Log4j2 {@link LogEventPatternConverter} used to help format optional values that should
* shown enclosed in square brackets. * be shown enclosed in square brackets.
* *
* @author Phillip Webb * @author Phillip Webb
* @since 3.4.0 * @since 3.4.0

View File

@ -22,7 +22,7 @@ import ch.qos.logback.core.pattern.CompositeConverter;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Logback {@link CompositeConverter} used help format optional values that should be * Logback {@link CompositeConverter} used to help format optional values that should be
* shown enclosed in square brackets. * shown enclosed in square brackets.
* *
* @author Phillip Webb * @author Phillip Webb

View File

@ -164,19 +164,19 @@ class LoggingSystemPropertiesTests {
} }
@Test @Test
void loggedApplicationGroupWhenHasApplicationGroup() { void applicationGroupWhenHasApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")).apply(null); new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isEqualTo("test"); assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isEqualTo("test");
} }
@Test @Test
void loggedApplicationGroupWhenHasNoApplicationGroup() { void applicationGroupWhenHasNoApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment()).apply(null); new LoggingSystemProperties(new MockEnvironment()).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull(); assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
} }
@Test @Test
void loggedApplicationGroupWhenApplicationGroupLoggingDisabled() { void applicationGroupWhenApplicationGroupLoggingDisabled() {
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test") new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")
.withProperty("logging.include-application-group", "false")).apply(null); .withProperty("logging.include-application-group", "false")).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull(); assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();

View File

@ -586,7 +586,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
void applicationNameLoggingToConsoleWhenDisabled(CapturedOutput output) { void applicationNameLoggingToConsoleWhenDisabled(CapturedOutput output) {
this.environment.setProperty("spring.application.name", "application-name"); this.environment.setProperty("spring.application.name", "myapp");
this.environment.setProperty("logging.include-application-name", "false"); this.environment.setProperty("logging.include-application-name", "false");
this.loggingSystem.setStandardConfigLocations(false); this.loggingSystem.setStandardConfigLocations(false);
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
@ -625,7 +625,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
void applicationNameLoggingToFileWhenDisabled() { void applicationNameLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.name", "application-name"); this.environment.setProperty("spring.application.name", "myapp");
this.environment.setProperty("logging.include-application-name", "false"); this.environment.setProperty("logging.include-application-name", "false");
new LoggingSystemProperties(this.environment).apply(); new LoggingSystemProperties(this.environment).apply();
File file = new File(tmpDir(), "log4j2-test.log"); File file = new File(tmpDir(), "log4j2-test.log");
@ -661,15 +661,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
void applicationGroupLoggingToConsoleWhenDisabled(CapturedOutput output) { void applicationGroupLoggingToConsoleWhenDisabled(CapturedOutput output) {
this.environment.setProperty("spring.application.group", "application-group"); this.environment.setProperty("spring.application.group", "mygroup");
this.environment.setProperty("logging.include-application-group", "false"); this.environment.setProperty("logging.include-application-group", "false");
this.loggingSystem.setStandardConfigLocations(false); this.loggingSystem.setStandardConfigLocations(false);
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null); this.loggingSystem.initialize(this.initializationContext, null, null);
this.logger.info("Hello world"); this.logger.info("Hello world");
assertThat(getLineWithText(output, "Hello world")).doesNotContain("${sys:LOGGED_APPLICATION_GROUP}") assertThat(getLineWithText(output, "Hello world")).doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("${sys:APPLICATION_GROUP}") .doesNotContain("mygroup");
.doesNotContain("myapp");
} }
@Test @Test
@ -700,7 +699,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
void applicationGroupLoggingToFileWhenDisabled() { void applicationGroupLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.group", "application-group"); this.environment.setProperty("spring.application.group", "mygroup");
this.environment.setProperty("logging.include-application-group", "false"); this.environment.setProperty("logging.include-application-group", "false");
new LoggingSystemProperties(this.environment).apply(); new LoggingSystemProperties(this.environment).apply();
File file = new File(tmpDir(), "log4j2-test.log"); File file = new File(tmpDir(), "log4j2-test.log");
@ -709,9 +708,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, logFile); this.loggingSystem.initialize(this.initializationContext, null, logFile);
this.logger.info("Hello world"); this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).doesNotContain("${sys:LOGGED_APPLICATION_GROUP}") assertThat(getLineWithText(file, "Hello world")).doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("${sys:APPLICATION_GROUP}") .doesNotContain("mygroup");
.doesNotContain("myapp");
} }
@Test @Test

View File

@ -20,7 +20,6 @@ import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -571,8 +570,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
Stream.of(LoggingSystemProperty.values()) Stream.of(LoggingSystemProperty.values())
.map(LoggingSystemProperty::getEnvironmentVariableName) .map(LoggingSystemProperty::getEnvironmentVariableName)
.forEach(expectedProperties::add); .forEach(expectedProperties::add);
expectedProperties expectedProperties.removeAll(List.of("LOG_FILE", "LOG_PATH"));
.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH", "LOGGED_APPLICATION_NAME", "LOGGED_APPLICATION_GROUP"));
expectedProperties.add("org.jboss.logging.provider"); expectedProperties.add("org.jboss.logging.provider");
expectedProperties.add("LOG_CORRELATION_PATTERN"); expectedProperties.add("LOG_CORRELATION_PATTERN");
expectedProperties.add("CONSOLE_LOG_STRUCTURED_FORMAT"); expectedProperties.add("CONSOLE_LOG_STRUCTURED_FORMAT");
@ -822,7 +820,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
void applicationNameLoggingToFileWhenDisabled(CapturedOutput output) { void applicationNameLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.name", "myapp"); this.environment.setProperty("spring.application.name", "myapp");
this.environment.setProperty("logging.include-application-name", "false"); this.environment.setProperty("logging.include-application-name", "false");
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");
@ -939,7 +937,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
} }
@Test @Test
void applicationGroupLoggingToFileWhenDisabled(CapturedOutput output) { void applicationGroupLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.group", "myGroup"); this.environment.setProperty("spring.application.group", "myGroup");
this.environment.setProperty("logging.include-application-group", "false"); this.environment.setProperty("logging.include-application-group", "false");
File file = new File(tmpDir(), "logback-test.log"); File file = new File(tmpDir(), "logback-test.log");

View File

@ -2,7 +2,7 @@
<Configuration status="WARN" monitorInterval="30"> <Configuration status="WARN" monitorInterval="30">
<Properties> <Properties>
<Property name="PID">????</Property> <Property name="PID">????</Property>
<Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_NAME:-}${sys:LOGGED_APPLICATION_GROUP:-}[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property> <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{${sys:APPLICATION_NAME:-}${sys:APPLICATION_GROUP:-}[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
</Properties> </Properties>
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true"> <Console name="Console" target="SYSTEM_OUT" follow="true">