Skip to content

Commit

Permalink
Merge pull request #42359 from izeye
Browse files Browse the repository at this point in the history
* pr/42359:
  Polish gh-39957 and gh-41444

Closes gh-42359
  • Loading branch information
snicoll committed Sep 19, 2024
2 parents e3aac5d + 94e8c5d commit 543bb80
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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].
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]
----
<Properties>
<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>
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public enum LoggingSystemProperty {

/**
* 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"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.apache.logging.log4j.core.pattern.PatternParser;

/**
* Log4j2 {@link LogEventPatternConverter} used help format optional values that should be
* shown enclosed in square brackets.
* Log4j2 {@link LogEventPatternConverter} used to help format optional values that should
* be shown enclosed in square brackets.
*
* @author Phillip Webb
* @since 3.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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.
*
* @author Phillip Webb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ void legacyLoggedApplicationNameWhenHasApplicationName() {
}

@Test
void loggedApplicationGroupWhenHasApplicationGroup() {
void applicationGroupWhenHasApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isEqualTo("test");
}

@Test
void loggedApplicationGroupWhenHasNoApplicationGroup() {
void applicationGroupWhenHasNoApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment()).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
}

@Test
void loggedApplicationGroupWhenApplicationGroupLoggingDisabled() {
void applicationGroupWhenApplicationGroupLoggingDisabled() {
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")
.withProperty("logging.include-application-group", "false")).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ void applicationNameLoggingToConsoleWhenHasApplicationNameWithParenthesis(Captur

@Test
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.loggingSystem.setStandardConfigLocations(false);
this.loggingSystem.beforeInitialize();
Expand Down Expand Up @@ -625,7 +625,7 @@ void applicationNameLoggingToFileWhenHasApplicationNameWithParenthesis() {

@Test
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");
new LoggingSystemProperties(this.environment).apply();
File file = new File(tmpDir(), "log4j2-test.log");
Expand Down Expand Up @@ -661,15 +661,14 @@ void applicationGroupLoggingToConsoleWhenHasApplicationGroupWithParenthesis(Capt

@Test
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.loggingSystem.setStandardConfigLocations(false);
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
this.logger.info("Hello world");
assertThat(getLineWithText(output, "Hello world")).doesNotContain("${sys:LOGGED_APPLICATION_GROUP}")
.doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("myapp");
assertThat(getLineWithText(output, "Hello world")).doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("mygroup");
}

@Test
Expand Down Expand Up @@ -700,7 +699,7 @@ void applicationGroupLoggingToFileWhenHasApplicationGroupWithParenthesis() {

@Test
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");
new LoggingSystemProperties(this.environment).apply();
File file = new File(tmpDir(), "log4j2-test.log");
Expand All @@ -709,9 +708,8 @@ void applicationGroupLoggingToFileWhenDisabled() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, logFile);
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).doesNotContain("${sys:LOGGED_APPLICATION_GROUP}")
.doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("myapp");
assertThat(getLineWithText(file, "Hello world")).doesNotContain("${sys:APPLICATION_GROUP}")
.doesNotContain("mygroup");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -571,8 +570,7 @@ void initializeShouldApplyLogbackSystemPropertiesToTheContext() {
Stream.of(LoggingSystemProperty.values())
.map(LoggingSystemProperty::getEnvironmentVariableName)
.forEach(expectedProperties::add);
expectedProperties
.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH", "LOGGED_APPLICATION_NAME", "LOGGED_APPLICATION_GROUP"));
expectedProperties.removeAll(List.of("LOG_FILE", "LOG_PATH"));
expectedProperties.add("org.jboss.logging.provider");
expectedProperties.add("LOG_CORRELATION_PATTERN");
expectedProperties.add("CONSOLE_LOG_STRUCTURED_FORMAT");
Expand Down Expand Up @@ -822,7 +820,7 @@ void applicationNameLoggingToFileWhenHasApplicationNameWithParenthesis() {
}

@Test
void applicationNameLoggingToFileWhenDisabled(CapturedOutput output) {
void applicationNameLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.name", "myapp");
this.environment.setProperty("logging.include-application-name", "false");
File file = new File(tmpDir(), "logback-test.log");
Expand Down Expand Up @@ -939,7 +937,7 @@ void applicationGroupLoggingToFileWhenHasApplicationGroupWithParenthesis() {
}

@Test
void applicationGroupLoggingToFileWhenDisabled(CapturedOutput output) {
void applicationGroupLoggingToFileWhenDisabled() {
this.environment.setProperty("spring.application.group", "myGroup");
this.environment.setProperty("logging.include-application-group", "false");
File file = new File(tmpDir(), "logback-test.log");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Configuration status="WARN" monitorInterval="30">
<Properties>
<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>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
Expand Down

0 comments on commit 543bb80

Please sign in to comment.