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 17b2e1a3aec9..ff14e81a4f55 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 @@ -239,7 +239,7 @@ protected void apply(LogFile logFile, PropertyResolver resolver) { setSystemProperty(LoggingSystemProperty.FILE_PATTERN, resolver); setSystemProperty(LoggingSystemProperty.LEVEL_PATTERN, resolver); setSystemProperty(LoggingSystemProperty.DATEFORMAT_PATTERN, resolver); - setSystemProperty(LoggingSystemProperty.CORRELATION_PATTERN, resolver); + setCorrelationPatternSystemProperty(resolver); if (logFile != null) { logFile.applyToSystemProperties(); } @@ -255,6 +255,12 @@ private void setApplicationNameSystemProperty(PropertyResolver resolver) { } } + private void setCorrelationPatternSystemProperty(PropertyResolver resolver) { + if (resolver.getProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, Boolean.class, Boolean.FALSE)) { + setSystemProperty(LoggingSystemProperty.CORRELATION_PATTERN, resolver); + } + } + private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) { setSystemProperty(property, resolver, null); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java index 2c5a1aaf2855..27cfd4a5d438 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemPropertiesTests.java @@ -118,17 +118,28 @@ private String getSystemProperty(LoggingSystemProperty property) { } @Test - void correlationPatternIsSet() { + void correlationPatternIsSetWhenExpectCorrelationIdTrue() { new LoggingSystemProperties( - new MockEnvironment().withProperty("logging.pattern.correlation", "correlation pattern")) + new MockEnvironment().withProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true") + .withProperty("logging.pattern.correlation", "correlation pattern")) .apply(null); assertThat(System.getProperty(LoggingSystemProperty.CORRELATION_PATTERN.getEnvironmentVariableName())) .isEqualTo("correlation pattern"); } + @Test + void correlationPatternIsNotSetWhenExpectCorrelationIdFalse() { + new LoggingSystemProperties( + new MockEnvironment().withProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "false") + .withProperty("logging.pattern.correlation", "correlation pattern")) + .apply(null); + assertThat(System.getProperty(LoggingSystemProperty.CORRELATION_PATTERN.getEnvironmentVariableName())).isNull(); + } + @Test void defaultValueResolverIsUsed() { - MockEnvironment environment = new MockEnvironment(); + MockEnvironment environment = new MockEnvironment().withProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, + "true"); Map defaultValues = Map .of(LoggingSystemProperty.CORRELATION_PATTERN.getApplicationPropertyName(), "default correlation pattern"); new LoggingSystemProperties(environment, defaultValues::get, null).apply(null); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 75564c7662e7..ca098b0313c0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -553,8 +553,9 @@ void correlationLoggingToConsoleWhenExpectCorrelationIdTrueAndNoMdcContent(Captu } @Test - void correlationLoggingToConsoleWhenHasCorrelationPattern(CapturedOutput output) { + void correlationLoggingToConsoleWhenHasCorrelationPatternAndExpectCorrelationIdTrue(CapturedOutput output) { this.environment.setProperty("logging.pattern.correlation", "%correlationId{spanId(0),traceId(0)}"); + this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true"); this.loggingSystem.setStandardConfigLocations(false); this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(this.initializationContext, null, null); @@ -564,6 +565,18 @@ void correlationLoggingToConsoleWhenHasCorrelationPattern(CapturedOutput output) .contains(" [0123456789012345-01234567890123456789012345678901] "); } + @Test + void correlationLoggingToConsoleWhenHasCorrelationPatternAndExpectCorrelationIdFalse(CapturedOutput output) { + this.environment.setProperty("logging.pattern.correlation", "%correlationId{spanId(0),traceId(0)}"); + this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "false"); + this.loggingSystem.setStandardConfigLocations(false); + this.loggingSystem.beforeInitialize(); + this.loggingSystem.initialize(this.initializationContext, null, null); + MDC.setContextMap(Map.of("traceId", "01234567890123456789012345678901", "spanId", "0123456789012345")); + this.logger.info("Hello world"); + assertThat(getLineWithText(output, "Hello world")).doesNotContain("0123456789012345"); + } + @Test void applicationNameLoggingWhenHasApplicationName(CapturedOutput output) { this.environment.setProperty("spring.application.name", "myapp"); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 845048e3473f..f8a7aa4b9d94 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -546,6 +546,7 @@ void initializeShouldApplyLogbackSystemPropertiesToTheContext() { this.environment.setProperty("logging.logback.rollingpolicy.max-file-size", "10MB"); this.environment.setProperty("logging.logback.rollingpolicy.total-size-cap", "100MB"); this.environment.setProperty("logging.logback.rollingpolicy.max-history", "20"); + this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true"); this.loggingSystem.beforeInitialize(); initialize(this.initializationContext, null, null); LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); @@ -729,8 +730,9 @@ void correlationLoggingToConsoleWhenExpectCorrelationIdTrueAndNoMdcContent(Captu } @Test - void correlationLoggingToConsoleWhenHasCorrelationPattern(CapturedOutput output) { + void correlationLoggingToConsoleWhenHasCorrelationPatternAndExpectCorrelationIdTrue(CapturedOutput output) { this.environment.setProperty("logging.pattern.correlation", "%correlationId{spanId(0),traceId(0)}"); + this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true"); initialize(this.initializationContext, null, null); MDC.setContextMap(Map.of("traceId", "01234567890123456789012345678901", "spanId", "0123456789012345")); this.logger.info("Hello world"); @@ -738,6 +740,16 @@ void correlationLoggingToConsoleWhenHasCorrelationPattern(CapturedOutput output) .contains(" [0123456789012345-01234567890123456789012345678901] "); } + @Test + void correlationLoggingToConsoleWhenHasCorrelationPatternAndExpectCorrelationIdFalse(CapturedOutput output) { + this.environment.setProperty("logging.pattern.correlation", "%correlationId{spanId(0),traceId(0)}"); + this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "false"); + initialize(this.initializationContext, null, null); + MDC.setContextMap(Map.of("traceId", "01234567890123456789012345678901", "spanId", "0123456789012345")); + this.logger.info("Hello world"); + assertThat(getLineWithText(output, "Hello world")).doesNotContain("0123456789012345"); + } + @Test void correlationLoggingToConsoleWhenUsingXmlConfiguration(CapturedOutput output) { this.environment.setProperty(LoggingSystem.EXPECT_CORRELATION_ID_PROPERTY, "true");