parent
e3aac5d442
commit
94e8c5db36
|
@ -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>
|
||||
----
|
||||
|
||||
|
|
|
@ -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"),
|
||||
|
||||
|
|
|
@ -29,8 +29,8 @@ import org.apache.logging.log4j.core.pattern.PatternFormatter;
|
|||
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
|
||||
|
|
|
@ -22,7 +22,7 @@ import ch.qos.logback.core.pattern.CompositeConverter;
|
|||
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
|
||||
|
|
|
@ -164,19 +164,19 @@ class LoggingSystemPropertiesTests {
|
|||
}
|
||||
|
||||
@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();
|
||||
|
|
|
@ -586,7 +586,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
|
||||
@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();
|
||||
|
@ -625,7 +625,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
|
||||
@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");
|
||||
|
@ -661,15 +661,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
|
||||
@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
|
||||
|
@ -700,7 +699,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
|
||||
@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");
|
||||
|
@ -709,9 +708,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
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
|
||||
|
|
|
@ -20,7 +20,6 @@ import java.io.File;
|
|||
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;
|
||||
|
@ -571,8 +570,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
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");
|
||||
|
@ -822,7 +820,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
}
|
||||
|
||||
@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");
|
||||
|
@ -939,7 +937,7 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||
}
|
||||
|
||||
@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");
|
||||
|
|
|
@ -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">
|
||||
|
|
Loading…
Reference in New Issue