Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set LOG_CORRELATION_PATTERN only when correlation ID is expected #39333

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> defaultValues = Map
.of(LoggingSystemProperty.CORRELATION_PATTERN.getApplicationPropertyName(), "default correlation pattern");
new LoggingSystemProperties(environment, defaultValues::get, null).apply(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -729,15 +730,26 @@ 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");
assertThat(getLineWithText(output, "Hello world"))
.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");
Expand Down