Add standardized property to distinguish a group of applications
This adds a property to provide some indicator that a set of applications are part of a larger "business application" so that they can be viewed in metrics, portals, traces and more. See gh-39957
This commit is contained in:
committed by
Moritz Halbritter
parent
edafc78375
commit
8ddb77f628
@@ -229,6 +229,7 @@ public class LoggingSystemProperties {
|
||||
protected void apply(LogFile logFile, PropertyResolver resolver) {
|
||||
String defaultCharsetName = getDefaultCharset().name();
|
||||
setApplicationNameSystemProperty(resolver);
|
||||
setApplicationGroupSystemProperty(resolver);
|
||||
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
|
||||
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
|
||||
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
|
||||
@@ -255,6 +256,16 @@ public class LoggingSystemProperties {
|
||||
}
|
||||
}
|
||||
|
||||
private void setApplicationGroupSystemProperty(PropertyResolver resolver) {
|
||||
if (resolver.getProperty("logging.include-application-group", Boolean.class, Boolean.TRUE)) {
|
||||
String applicationGroup = resolver.getProperty("spring.application.group");
|
||||
if (StringUtils.hasText(applicationGroup)) {
|
||||
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName(),
|
||||
"[%s] ".formatted(applicationGroup));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
|
||||
setSystemProperty(property, resolver, Function.identity());
|
||||
}
|
||||
|
||||
@@ -30,6 +30,11 @@ public enum LoggingSystemProperty {
|
||||
*/
|
||||
APPLICATION_NAME("LOGGED_APPLICATION_NAME"),
|
||||
|
||||
/**
|
||||
* Logging system property for the application group that should be logged.
|
||||
*/
|
||||
APPLICATION_GROUP("LOGGED_APPLICATION_GROUP"),
|
||||
|
||||
/**
|
||||
* Logging system property for the process ID.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.pattern.PropertyConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
|
||||
import org.springframework.boot.logging.LoggingSystemProperty;
|
||||
|
||||
/**
|
||||
* Logback {@link ClassicConverter} to convert the
|
||||
* {@link LoggingSystemProperty#APPLICATION_GROUP APPLICATION_GROUP} into a value suitable
|
||||
* for logging. Similar to Logback's {@link PropertyConverter} but a non-existent property
|
||||
* is logged as an empty string rather than {@code null}.
|
||||
*
|
||||
* @author Jakob Wanger
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public class ApplicationGroupConverter extends ClassicConverter {
|
||||
|
||||
@Override
|
||||
public String convert(ILoggingEvent event) {
|
||||
String applicationGroup = event.getLoggerContextVO()
|
||||
.getPropertyMap()
|
||||
.get(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
|
||||
if (applicationGroup == null) {
|
||||
applicationGroup = System.getProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
|
||||
if (applicationGroup == null) {
|
||||
applicationGroup = "";
|
||||
}
|
||||
}
|
||||
return applicationGroup;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,6 +69,7 @@ class DefaultLogbackConfiguration {
|
||||
|
||||
private void defaults(LogbackConfigurator config) {
|
||||
config.conversionRule("applicationName", ApplicationNameConverter.class);
|
||||
config.conversionRule("applicationGroup", ApplicationGroupConverter.class);
|
||||
config.conversionRule("clr", ColorConverter.class);
|
||||
config.conversionRule("correlationId", CorrelationIdConverter.class);
|
||||
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class);
|
||||
@@ -76,7 +77,7 @@ class DefaultLogbackConfiguration {
|
||||
config.getContext()
|
||||
.putProperty("CONSOLE_LOG_PATTERN", resolve(config, "${CONSOLE_LOG_PATTERN:-"
|
||||
+ "%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) "
|
||||
+ "%clr(${PID:- }){magenta} %clr(---){faint} %clr(%applicationName[%15.15t]){faint} "
|
||||
+ "%clr(${PID:- }){magenta} %clr(---){faint} %clr(%applicationName[%15.15t]){faint} %clr(---){faint} %clr(%applicationGroup[%15.15t]){faint} "
|
||||
+ "%clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} "
|
||||
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"));
|
||||
String defaultCharset = Charset.defaultCharset().name();
|
||||
@@ -85,7 +86,7 @@ class DefaultLogbackConfiguration {
|
||||
config.getContext().putProperty("CONSOLE_LOG_THRESHOLD", resolve(config, "${CONSOLE_LOG_THRESHOLD:-TRACE}"));
|
||||
config.getContext()
|
||||
.putProperty("FILE_LOG_PATTERN", resolve(config, "${FILE_LOG_PATTERN:-"
|
||||
+ "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- %applicationName[%t] "
|
||||
+ "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- %applicationName[%t] --- %applicationGroup[%t] "
|
||||
+ "${LOG_CORRELATION_PATTERN:-}"
|
||||
+ "%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"));
|
||||
config.getContext()
|
||||
|
||||
@@ -58,9 +58,9 @@ class LogbackRuntimeHints implements RuntimeHintsRegistrar {
|
||||
}
|
||||
|
||||
private void registerHintsForSpringBootConverters(ReflectionHints reflection) {
|
||||
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class, ColorConverter.class,
|
||||
ExtendedWhitespaceThrowableProxyConverter.class, WhitespaceThrowableProxyConverter.class,
|
||||
CorrelationIdConverter.class);
|
||||
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class,
|
||||
ApplicationGroupConverter.class, ColorConverter.class, ExtendedWhitespaceThrowableProxyConverter.class,
|
||||
WhitespaceThrowableProxyConverter.class, CorrelationIdConverter.class);
|
||||
}
|
||||
|
||||
private void registerForPublicConstructorInvocation(ReflectionHints reflection, Class<?>... classes) {
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd'T'HH:mm:ss.SSSXXX</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_NAME:-}[%15.15t]}{faint} %clr{${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- ${sys:LOGGED_APPLICATION_NAME:-}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_NAME:-}[%15.15t]}{faint} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_GROUP:-}[%15.15t]}{faint} %clr{${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- ${sys:LOGGED_APPLICATION_NAME:-} --- ${sys:LOGGED_APPLICATION_GROUP:-}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT" follow="true">
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
|
||||
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
|
||||
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd'T'HH:mm:ss.SSSXXX</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_NAME:-}[%15.15t]}{faint} %clr{${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- ${sys:LOGGED_APPLICATION_NAME:-}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_NAME:-}[%15.15t]} {faint}%clr{---}{faint} %clr{${sys:LOGGED_APPLICATION_GROUP:-}[%15.15t]}{faint} %clr{${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- ${sys:LOGGED_APPLICATION_NAME:-} --- ${sys:LOGGED_APPLICATION_GROUP:-}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT" follow="true">
|
||||
|
||||
@@ -6,15 +6,16 @@ Default logback configuration provided for import
|
||||
|
||||
<included>
|
||||
<conversionRule conversionWord="applicationName" converterClass="org.springframework.boot.logging.logback.ApplicationNameConverter" />
|
||||
<conversionRule conversionWord="applicationGroup" converterClass="org.springframework.boot.logging.logback.ApplicationGroupConverter" />
|
||||
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
|
||||
<conversionRule conversionWord="correlationId" converterClass="org.springframework.boot.logging.logback.CorrelationIdConverter" />
|
||||
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
|
||||
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
|
||||
|
||||
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr(%applicationName[%15.15t]){faint} %clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
||||
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr(%applicationName[%15.15t]){faint} %clr(---){faint} %clr(%applicationGroup[%15.15t]){faint} %clr(${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
||||
<property name="CONSOLE_LOG_CHARSET" value="${CONSOLE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/>
|
||||
<property name="CONSOLE_LOG_THRESHOLD" value="${CONSOLE_LOG_THRESHOLD:-TRACE}"/>
|
||||
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- %applicationName[%t] ${LOG_CORRELATION_PATTERN:-}%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
||||
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- %applicationName[%t] --- %applicationGroup[%t] ${LOG_CORRELATION_PATTERN:-}%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
|
||||
<property name="FILE_LOG_CHARSET" value="${FILE_LOG_CHARSET:-${file.encoding:-UTF-8}}"/>
|
||||
<property name="FILE_LOG_THRESHOLD" value="${FILE_LOG_THRESHOLD:-TRACE}"/>
|
||||
|
||||
|
||||
@@ -156,6 +156,25 @@ class LoggingSystemPropertiesTests {
|
||||
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_NAME)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void loggedApplicationGroupWhenHasApplicationGroup() {
|
||||
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")).apply(null);
|
||||
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isEqualTo("[test] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void loggedApplicationGroupWhenHasNoApplicationGroup() {
|
||||
new LoggingSystemProperties(new MockEnvironment()).apply(null);
|
||||
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void loggedApplicationGroupWhenApplicationGroupLoggingDisabled() {
|
||||
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")
|
||||
.withProperty("logging.include-application-group", "false")).apply(null);
|
||||
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportFalseConsoleThreshold() {
|
||||
new LoggingSystemProperties(new MockEnvironment().withProperty("logging.threshold.console", "false"))
|
||||
|
||||
@@ -651,6 +651,79 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
||||
.doesNotContain("myapp");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenHasApplicationGroup(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "mygroup");
|
||||
this.loggingSystem.setStandardConfigLocations(false);
|
||||
this.loggingSystem.beforeInitialize();
|
||||
this.loggingSystem.initialize(this.initializationContext, null, null);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(output, "Hello world")).contains("[mygroup] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenHasApplicationGroupWithParenthesis(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "mygroup (dev)");
|
||||
this.loggingSystem.setStandardConfigLocations(false);
|
||||
this.loggingSystem.beforeInitialize();
|
||||
this.loggingSystem.initialize(this.initializationContext, null, null);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(output, "Hello world")).contains("[mygroup (dev)] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenDisabled(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "application-group");
|
||||
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("myapp");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenHasApplicationGroup() {
|
||||
this.environment.setProperty("spring.application.group", "mygroup");
|
||||
new LoggingSystemProperties(this.environment).apply();
|
||||
File file = new File(tmpDir(), "log4j2-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
this.loggingSystem.setStandardConfigLocations(false);
|
||||
this.loggingSystem.beforeInitialize();
|
||||
this.loggingSystem.initialize(this.initializationContext, null, logFile);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(file, "Hello world")).contains("[mygroup] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenHasApplicationGroupWithParenthesis() {
|
||||
this.environment.setProperty("spring.application.group", "mygroup (dev)");
|
||||
new LoggingSystemProperties(this.environment).apply();
|
||||
File file = new File(tmpDir(), "log4j2-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
this.loggingSystem.setStandardConfigLocations(false);
|
||||
this.loggingSystem.beforeInitialize();
|
||||
this.loggingSystem.initialize(this.initializationContext, null, logFile);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(file, "Hello world")).contains("[mygroup (dev)] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenDisabled() {
|
||||
this.environment.setProperty("spring.application.group", "application-group");
|
||||
this.environment.setProperty("logging.include-application-group", "false");
|
||||
new LoggingSystemProperties(this.environment).apply();
|
||||
File file = new File(tmpDir(), "log4j2-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
this.loggingSystem.setStandardConfigLocations(false);
|
||||
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("myapp");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotContainAnsiEscapeCodes(CapturedOutput output) {
|
||||
this.loggingSystem.beforeInitialize();
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.logging.logback;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.spi.LoggerContextVO;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.logging.LoggingSystemProperty;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link ApplicationGroupConverter}.
|
||||
*
|
||||
* @author Jakob Wanger
|
||||
*/
|
||||
class ApplicationGroupConverterTests {
|
||||
|
||||
private final ApplicationGroupConverter converter;
|
||||
|
||||
private final LoggingEvent event = new LoggingEvent();
|
||||
|
||||
ApplicationGroupConverterTests() {
|
||||
this.converter = new ApplicationGroupConverter();
|
||||
this.converter.setContext(new LoggerContext());
|
||||
this.event.setLoggerContextRemoteView(
|
||||
new LoggerContextVO("test", Collections.emptyMap(), System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenNoLoggedApplicationGroupConvertReturnsEmptyString() {
|
||||
withLoggedApplicationGroup(null, () -> {
|
||||
this.converter.start();
|
||||
String converted = this.converter.convert(this.event);
|
||||
assertThat(converted).isEqualTo("");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenLoggedApplicationGroupConvertReturnsIt() {
|
||||
withLoggedApplicationGroup("my-application", () -> {
|
||||
this.converter.start();
|
||||
String converted = this.converter.convert(this.event);
|
||||
assertThat(converted).isEqualTo("my-application");
|
||||
});
|
||||
}
|
||||
|
||||
private void withLoggedApplicationGroup(String group, Runnable action) {
|
||||
if (group == null) {
|
||||
System.clearProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
|
||||
}
|
||||
else {
|
||||
System.setProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName(), group);
|
||||
}
|
||||
try {
|
||||
action.run();
|
||||
}
|
||||
finally {
|
||||
System.clearProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -876,6 +876,62 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
||||
assertThat(output).doesNotContain("WARN");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenHasApplicationGroup(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "mygroup");
|
||||
initialize(this.initializationContext, null, null);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(output, "Hello world")).contains("[mygroup] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenHasApplicationGroupWithParenthesis(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "mygroup (dev)");
|
||||
initialize(this.initializationContext, null, null);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(output, "Hello world")).contains("[mygroup (dev)] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToConsoleWhenDisabled(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "mygroup");
|
||||
this.environment.setProperty("logging.include-application-group", "false");
|
||||
initialize(this.initializationContext, null, null);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(output, "Hello world")).doesNotContain("mygroup").doesNotContain("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenHasApplicationGroup() {
|
||||
this.environment.setProperty("spring.application.group", "mygroup");
|
||||
File file = new File(tmpDir(), "logback-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
initialize(this.initializationContext, null, logFile);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(file, "Hello world")).contains("[mygroup] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenHasApplicationGroupWithParenthesis() {
|
||||
this.environment.setProperty("spring.application.group", "mygroup (dev)");
|
||||
File file = new File(tmpDir(), "logback-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
initialize(this.initializationContext, null, logFile);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(file, "Hello world")).contains("[mygroup (dev)] ");
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationGroupLoggingToFileWhenDisabled(CapturedOutput output) {
|
||||
this.environment.setProperty("spring.application.group", "myGroup");
|
||||
this.environment.setProperty("logging.include-application-group", "false");
|
||||
File file = new File(tmpDir(), "logback-test.log");
|
||||
LogFile logFile = getLogFile(file.getPath(), null);
|
||||
initialize(this.initializationContext, null, logFile);
|
||||
this.logger.info("Hello world");
|
||||
assertThat(getLineWithText(file, "Hello world")).doesNotContain("myGroup").doesNotContain("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotContainAnsiEscapeCodes(CapturedOutput output) {
|
||||
this.loggingSystem.beforeInitialize();
|
||||
|
||||
Reference in New Issue
Block a user