Skip to content

Commit c3ad8b0

Browse files
committed
Add APPLICATION_NAME and APPLICATION_GROUP logging properties
Add `APPLICATION_NAME` and `APPLICATION_GROUP` properties that contain verbatim values rather than formatted strings. Formatting of the values is now handled by new `EnclosedInSquareBracketsConverter` classes for both Logback and Log4J2. The existing `LOGGED_APPLICATION_NAME` variable is now considered deprecated. The `LOGGED_APPLICATION_GROUP` variable and related logback converter have been removed since they never made it to a GA release. Closes gh-41444
1 parent db5830a commit c3ad8b0

File tree

18 files changed

+283
-107
lines changed

18 files changed

+283
-107
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private PropertyResolver getPropertyResolver() {
117117

118118
protected void apply(LogFile logFile, PropertyResolver resolver) {
119119
String defaultCharsetName = getDefaultCharset().name();
120-
setApplicationNameSystemProperty(resolver);
121-
setApplicationGroupSystemProperty(resolver);
120+
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME, resolver);
121+
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP, resolver);
122122
setSystemProperty(LoggingSystemProperty.PID, new ApplicationPid().toString());
123123
setSystemProperty(LoggingSystemProperty.CONSOLE_CHARSET, resolver, defaultCharsetName);
124124
setSystemProperty(LoggingSystemProperty.FILE_CHARSET, resolver, defaultCharsetName);
@@ -135,26 +135,6 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
135135
}
136136
}
137137

138-
private void setApplicationNameSystemProperty(PropertyResolver resolver) {
139-
if (resolver.getProperty("logging.include-application-name", Boolean.class, Boolean.TRUE)) {
140-
String applicationName = resolver.getProperty("spring.application.name");
141-
if (StringUtils.hasText(applicationName)) {
142-
setSystemProperty(LoggingSystemProperty.APPLICATION_NAME.getEnvironmentVariableName(),
143-
"[%s] ".formatted(applicationName));
144-
}
145-
}
146-
}
147-
148-
private void setApplicationGroupSystemProperty(PropertyResolver resolver) {
149-
if (resolver.getProperty("logging.include-application-group", Boolean.class, Boolean.TRUE)) {
150-
String applicationGroup = resolver.getProperty("spring.application.group");
151-
if (StringUtils.hasText(applicationGroup)) {
152-
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName(),
153-
"[%s] ".formatted(applicationGroup));
154-
}
155-
}
156-
}
157-
158138
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver) {
159139
setSystemProperty(property, resolver, Function.identity());
160140
}
@@ -170,11 +150,21 @@ private void setSystemProperty(LoggingSystemProperty property, PropertyResolver
170150

171151
private void setSystemProperty(LoggingSystemProperty property, PropertyResolver resolver, String defaultValue,
172152
Function<String, String> mapper) {
153+
if (property.getIncludePropertyName() != null) {
154+
if (!resolver.getProperty(property.getIncludePropertyName(), Boolean.class, Boolean.TRUE)) {
155+
return;
156+
}
157+
}
173158
String value = (property.getApplicationPropertyName() != null)
174159
? resolver.getProperty(property.getApplicationPropertyName()) : null;
175160
value = (value != null) ? value : this.defaultValueResolver.apply(property.getApplicationPropertyName());
176161
value = (value != null) ? value : defaultValue;
177-
setSystemProperty(property.getEnvironmentVariableName(), mapper.apply(value));
162+
value = mapper.apply(value);
163+
setSystemProperty(property.getEnvironmentVariableName(), value);
164+
if (property == LoggingSystemProperty.APPLICATION_NAME && StringUtils.hasText(value)) {
165+
// LOGGED_APPLICATION_NAME is deprecated for removal in 3.6.0
166+
setSystemProperty("LOGGED_APPLICATION_NAME", "[%s] ".formatted(value));
167+
}
178168
}
179169

180170
private void setSystemProperty(LoggingSystemProperty property, String value) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public enum LoggingSystemProperty {
2828
/**
2929
* Logging system property for the application name that should be logged.
3030
*/
31-
APPLICATION_NAME("LOGGED_APPLICATION_NAME"),
31+
APPLICATION_NAME("APPLICATION_NAME", "spring.application.name", "logging.include-application-name"),
3232

3333
/**
3434
* Logging system property for the application group that should be logged.
3535
*/
36-
APPLICATION_GROUP("LOGGED_APPLICATION_GROUP"),
36+
APPLICATION_GROUP("APPLICATION_GROUP", "spring.application.group", "logging.include-application-group"),
3737

3838
/**
3939
* Logging system property for the process ID.
@@ -104,13 +104,20 @@ public enum LoggingSystemProperty {
104104

105105
private final String applicationPropertyName;
106106

107+
private final String includePropertyName;
108+
107109
LoggingSystemProperty(String environmentVariableName) {
108110
this(environmentVariableName, null);
109111
}
110112

111113
LoggingSystemProperty(String environmentVariableName, String applicationPropertyName) {
114+
this(environmentVariableName, applicationPropertyName, null);
115+
}
116+
117+
LoggingSystemProperty(String environmentVariableName, String applicationPropertyName, String includePropertyName) {
112118
this.environmentVariableName = environmentVariableName;
113119
this.applicationPropertyName = applicationPropertyName;
120+
this.includePropertyName = includePropertyName;
114121
}
115122

116123
/**
@@ -125,4 +132,8 @@ String getApplicationPropertyName() {
125132
return this.applicationPropertyName;
126133
}
127134

135+
String getIncludePropertyName() {
136+
return this.includePropertyName;
137+
}
138+
128139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.log4j2;
18+
19+
import java.util.List;
20+
21+
import org.apache.logging.log4j.core.LogEvent;
22+
import org.apache.logging.log4j.core.config.Configuration;
23+
import org.apache.logging.log4j.core.config.plugins.Plugin;
24+
import org.apache.logging.log4j.core.layout.PatternLayout;
25+
import org.apache.logging.log4j.core.pattern.ConverterKeys;
26+
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
27+
import org.apache.logging.log4j.core.pattern.PatternConverter;
28+
import org.apache.logging.log4j.core.pattern.PatternFormatter;
29+
import org.apache.logging.log4j.core.pattern.PatternParser;
30+
31+
/**
32+
* Log4j2 {@link LogEventPatternConverter} used help format optional values that should be
33+
* shown enclosed in square brackets.
34+
*
35+
* @author Phillip Webb
36+
* @since 3.4.0
37+
*/
38+
@Plugin(name = "enclosedInSquareBrackets", category = PatternConverter.CATEGORY)
39+
@ConverterKeys("esb")
40+
public final class EnclosedInSquareBracketsConverter extends LogEventPatternConverter {
41+
42+
private final List<PatternFormatter> formatters;
43+
44+
private EnclosedInSquareBracketsConverter(List<PatternFormatter> formatters) {
45+
super("enclosedInSquareBrackets", null);
46+
this.formatters = formatters;
47+
}
48+
49+
@Override
50+
public void format(LogEvent event, StringBuilder toAppendTo) {
51+
StringBuilder buf = new StringBuilder();
52+
for (PatternFormatter formatter : this.formatters) {
53+
formatter.format(event, buf);
54+
}
55+
if (buf.isEmpty()) {
56+
return;
57+
}
58+
toAppendTo.append("[");
59+
toAppendTo.append(buf);
60+
toAppendTo.append("] ");
61+
}
62+
63+
/**
64+
* Creates a new instance of the class. Required by Log4J2.
65+
* @param config the configuration
66+
* @param options the options
67+
* @return a new instance, or {@code null} if the options are invalid
68+
*/
69+
public static EnclosedInSquareBracketsConverter newInstance(Configuration config, String[] options) {
70+
if (options.length < 1) {
71+
LOGGER.error("Incorrect number of options on style. Expected at least 1, received {}", options.length);
72+
return null;
73+
}
74+
PatternParser parser = PatternLayout.createPatternParser(config);
75+
List<PatternFormatter> formatters = parser.parse(options[0]);
76+
return new EnclosedInSquareBracketsConverter(formatters);
77+
}
78+
79+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationGroupConverter.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationNameConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
* @author Andy Wilkinson
3232
* @author Phillip Webb
3333
* @since 3.2.4
34+
* @deprecated since 3.4.0 for removal in 3.6.0 in favor of
35+
* {@link EnclosedInSquareBracketsConverter}
3436
*/
37+
@Deprecated(since = "3.4.0", forRemoval = true)
3538
public class ApplicationNameConverter extends ClassicConverter {
3639

3740
private static final String ENVIRONMENT_VARIABLE_NAME = LoggingSystemProperty.APPLICATION_NAME

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DefaultLogbackConfiguration {
5252

5353
private static String DEFAULT_CHARSET = Charset.defaultCharset().name();
5454

55-
private static String NAME_AND_GROUP = "%applicationName%applicationGroup";
55+
private static final String NAME_AND_GROUP = "%esb(){APPLICATION_NAME}%esb{APPLICATION_GROUP}";
5656

5757
private static String DATETIME = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}";
5858

@@ -90,10 +90,10 @@ void apply(LogbackConfigurator config) {
9090
}
9191

9292
private void defaults(LogbackConfigurator config) {
93-
config.conversionRule("applicationGroup", ApplicationGroupConverter.class);
94-
config.conversionRule("applicationName", ApplicationNameConverter.class);
93+
deprecatedDefaults(config);
9594
config.conversionRule("clr", ColorConverter.class);
9695
config.conversionRule("correlationId", CorrelationIdConverter.class);
96+
config.conversionRule("esb", EnclosedInSquareBracketsConverter.class);
9797
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class);
9898
config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class);
9999
putProperty(config, "CONSOLE_LOG_PATTERN", CONSOLE_LOG_PATTERN);
@@ -109,7 +109,12 @@ private void defaults(LogbackConfigurator config) {
109109
config.logger("org.apache.tomcat.util.net.NioSelectorPool", Level.WARN);
110110
config.logger("org.eclipse.jetty.util.component.AbstractLifeCycle", Level.ERROR);
111111
config.logger("org.hibernate.validator.internal.util.Version", Level.WARN);
112-
config.logger("org.springframework.boot.actuate.endpoint.jmx", Level.WARN);// @formatter:on
112+
config.logger("org.springframework.boot.actuate.endpoint.jmx", Level.WARN);
113+
}
114+
115+
@SuppressWarnings("removal")
116+
private void deprecatedDefaults(LogbackConfigurator config) {
117+
config.conversionRule("applicationName", ApplicationNameConverter.class);
113118
}
114119

115120
void putProperty(LogbackConfigurator config, String name, String val) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.logback;
18+
19+
import ch.qos.logback.classic.spi.ILoggingEvent;
20+
import ch.qos.logback.core.pattern.CompositeConverter;
21+
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* Logback {@link CompositeConverter} used help format optional values that should be
26+
* shown enclosed in square brackets.
27+
*
28+
* @author Phillip Webb
29+
* @since 3.4.0
30+
*/
31+
public class EnclosedInSquareBracketsConverter extends CompositeConverter<ILoggingEvent> {
32+
33+
@Override
34+
protected String transform(ILoggingEvent event, String in) {
35+
in = (!StringUtils.hasLength(in)) ? resolveFromFirstOption(event) : in;
36+
return (!StringUtils.hasLength(in)) ? "" : "[%s] ".formatted(in);
37+
}
38+
39+
private String resolveFromFirstOption(ILoggingEvent event) {
40+
String name = getFirstOption();
41+
if (name == null) {
42+
return null;
43+
}
44+
String value = event.getLoggerContextVO().getPropertyMap().get(name);
45+
return (value != null) ? value : System.getProperty(name);
46+
}
47+
48+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackRuntimeHints.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
4444
registerHintsForLogbackLoggingSystemTypeChecks(reflection, classLoader);
4545
registerHintsForBuiltInLogbackConverters(reflection);
4646
registerHintsForSpringBootConverters(reflection);
47+
registerHintsForDeprecateSpringBootConverters(reflection);
4748
}
4849

4950
private void registerHintsForLogbackLoggingSystemTypeChecks(ReflectionHints reflection, ClassLoader classLoader) {
@@ -58,11 +59,16 @@ private void registerHintsForBuiltInLogbackConverters(ReflectionHints reflection
5859
}
5960

6061
private void registerHintsForSpringBootConverters(ReflectionHints reflection) {
61-
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class,
62-
ApplicationGroupConverter.class, ColorConverter.class, ExtendedWhitespaceThrowableProxyConverter.class,
62+
registerForPublicConstructorInvocation(reflection, ColorConverter.class,
63+
EnclosedInSquareBracketsConverter.class, ExtendedWhitespaceThrowableProxyConverter.class,
6364
WhitespaceThrowableProxyConverter.class, CorrelationIdConverter.class);
6465
}
6566

67+
@SuppressWarnings("removal")
68+
private void registerHintsForDeprecateSpringBootConverters(ReflectionHints reflection) {
69+
registerForPublicConstructorInvocation(reflection, ApplicationNameConverter.class);
70+
}
71+
6672
private void registerForPublicConstructorInvocation(ReflectionHints reflection, Class<?>... classes) {
6773
reflection.registerTypes(TypeReference.listOf(classes),
6874
(hint) -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
55
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
66
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd'T'HH:mm:ss.SSSXXX</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{--- ${sys:LOGGED_APPLICATION_NAME:-}${sys:LOGGED_APPLICATION_GROUP:-}[%15.15t] ${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8-
<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>
7+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{--- %esb{${sys:APPLICATION_NAME:-}}%esb{${sys:APPLICATION_GROUP:-}}[%15.15t] ${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8+
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- %esb{${sys:APPLICATION_NAME:-}}%esb{${sys:APPLICATION_GROUP:-}}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
99
</Properties>
1010
<Appenders>
1111
<Console name="Console" target="SYSTEM_OUT" follow="true">

spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
55
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
66
<Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd'T'HH:mm:ss.SSSXXX</Property>
7-
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{--- ${sys:LOGGED_APPLICATION_NAME:-}${sys:LOGGED_APPLICATION_GROUP:-}[%15.15t] ${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8-
<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>
7+
<Property name="CONSOLE_LOG_PATTERN">%clr{%d{${sys:LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${sys:LOG_LEVEL_PATTERN}} %clr{%pid}{magenta} %clr{--- %esb{${sys:APPLICATION_NAME:-}}%esb{${sys:APPLICATION_GROUP:-}}[%15.15t] ${sys:LOG_CORRELATION_PATTERN:-}}{faint}%clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
8+
<Property name="FILE_LOG_PATTERN">%d{${sys:LOG_DATEFORMAT_PATTERN}} ${sys:LOG_LEVEL_PATTERN} %pid --- %esb{${sys:APPLICATION_NAME:-}}%esb{${sys:APPLICATION_GROUP:-}}[%t] ${sys:LOG_CORRELATION_PATTERN:-}%-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
99
</Properties>
1010
<Appenders>
1111
<Console name="Console" target="SYSTEM_OUT" follow="true">

0 commit comments

Comments
 (0)