Polish logging related code

This commit is contained in:
Phillip Webb 2024-07-10 15:15:29 -07:00
parent 653443adc1
commit db5830a2e0
15 changed files with 191 additions and 125 deletions

View File

@ -149,7 +149,7 @@ public class LoggingSystemProperties {
if (resolver.getProperty("logging.include-application-group", Boolean.class, Boolean.TRUE)) {
String applicationGroup = resolver.getProperty("spring.application.group");
if (StringUtils.hasText(applicationGroup)) {
setSystemProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName(),
setSystemProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName(),
"[%s] ".formatted(applicationGroup));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
@ -33,7 +33,7 @@ public enum LoggingSystemProperty {
/**
* Logging system property for the application group that should be logged.
*/
LOGGED_APPLICATION_GROUP("LOGGED_APPLICATION_GROUP"),
APPLICATION_GROUP("LOGGED_APPLICATION_GROUP"),
/**
* Logging system property for the process ID.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
@ -16,6 +16,7 @@
package org.springframework.boot.logging.log4j2;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -38,7 +39,7 @@ import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
/**
* Log4j2 {@link LogEventPatternConverter} colors output using the {@link AnsiOutput}
* Log4j2 {@link LogEventPatternConverter} to color output using the {@link AnsiOutput}
* class. A single option 'styling' can be provided to the converter, or if not specified
* color styling will be picked based on the logging level.
*
@ -53,23 +54,10 @@ public final class ColorConverter extends LogEventPatternConverter {
static {
Map<String, AnsiElement> ansiElements = new HashMap<>();
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("white", AnsiColor.WHITE);
Arrays.stream(AnsiColor.values())
.filter((color) -> color != AnsiColor.DEFAULT)
.forEach((color) -> ansiElements.put(color.name().toLowerCase(), color));
ansiElements.put("faint", AnsiStyle.FAINT);
ansiElements.put("red", AnsiColor.RED);
ansiElements.put("green", AnsiColor.GREEN);
ansiElements.put("yellow", AnsiColor.YELLOW);
ansiElements.put("blue", AnsiColor.BLUE);
ansiElements.put("magenta", AnsiColor.MAGENTA);
ansiElements.put("cyan", AnsiColor.CYAN);
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
ELEMENTS = Collections.unmodifiableMap(ansiElements);
}
@ -93,27 +81,6 @@ public final class ColorConverter extends LogEventPatternConverter {
this.styling = styling;
}
/**
* Creates a new instance of the class. Required by Log4J2.
* @param config the configuration
* @param options the options
* @return a new instance, or {@code null} if the options are invalid
*/
public static ColorConverter newInstance(Configuration config, String[] options) {
if (options.length < 1) {
LOGGER.error("Incorrect number of options on style. Expected at least 1, received {}", options.length);
return null;
}
if (options[0] == null) {
LOGGER.error("No pattern supplied on style");
return null;
}
PatternParser parser = PatternLayout.createPatternParser(config);
List<PatternFormatter> formatters = parser.parse(options[0]);
AnsiElement element = (options.length != 1) ? ELEMENTS.get(options[1]) : null;
return new ColorConverter(formatters, element);
}
@Override
public boolean handlesThrowable() {
for (PatternFormatter formatter : this.formatters) {
@ -145,4 +112,25 @@ public final class ColorConverter extends LogEventPatternConverter {
toAppendTo.append(AnsiOutput.toString(element, in));
}
/**
* Creates a new instance of the class. Required by Log4J2.
* @param config the configuration
* @param options the options
* @return a new instance, or {@code null} if the options are invalid
*/
public static ColorConverter newInstance(Configuration config, String[] options) {
if (options.length < 1) {
LOGGER.error("Incorrect number of options on style. Expected at least 1, received {}", options.length);
return null;
}
if (options[0] == null) {
LOGGER.error("No pattern supplied on style");
return null;
}
PatternParser parser = PatternLayout.createPatternParser(config);
List<PatternFormatter> formatters = parser.parse(options[0]);
AnsiElement element = (options.length != 1) ? ELEMENTS.get(options[1]) : null;
return new ColorConverter(formatters, element);
}
}

View File

@ -24,7 +24,7 @@ import org.springframework.boot.logging.LoggingSystemProperty;
/**
* Logback {@link ClassicConverter} to convert the
* {@link LoggingSystemProperty#LOGGED_APPLICATION_GROUP APPLICATION_GROUP} into a value
* {@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}.
*
@ -33,19 +33,15 @@ import org.springframework.boot.logging.LoggingSystemProperty;
*/
public class ApplicationGroupConverter extends ClassicConverter {
private static final String ENVIRONMENT_VARIABLE_NAME = LoggingSystemProperty.APPLICATION_GROUP
.getEnvironmentVariableName();
@Override
public String convert(ILoggingEvent event) {
String applicationGroup = event.getLoggerContextVO()
.getPropertyMap()
.get(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName());
if (applicationGroup == null) {
applicationGroup = System
.getProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName());
if (applicationGroup == null) {
applicationGroup = "";
}
}
return applicationGroup;
String applicationGroup = event.getLoggerContextVO().getPropertyMap().get(ENVIRONMENT_VARIABLE_NAME);
applicationGroup = (applicationGroup != null) ? applicationGroup
: System.getProperty(ENVIRONMENT_VARIABLE_NAME);
return (applicationGroup != null) ? applicationGroup : "";
}
}

View File

@ -29,22 +29,19 @@ import org.springframework.boot.logging.LoggingSystemProperty;
* is logged as an empty string rather than {@code null}.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @since 3.2.4
*/
public class ApplicationNameConverter extends ClassicConverter {
private static final String ENVIRONMENT_VARIABLE_NAME = LoggingSystemProperty.APPLICATION_NAME
.getEnvironmentVariableName();
@Override
public String convert(ILoggingEvent event) {
String applicationName = event.getLoggerContextVO()
.getPropertyMap()
.get(LoggingSystemProperty.APPLICATION_NAME.getEnvironmentVariableName());
if (applicationName == null) {
applicationName = System.getProperty(LoggingSystemProperty.APPLICATION_NAME.getEnvironmentVariableName());
if (applicationName == null) {
applicationName = "";
}
}
return applicationName;
String applicationName = event.getLoggerContextVO().getPropertyMap().get(ENVIRONMENT_VARIABLE_NAME);
applicationName = (applicationName != null) ? applicationName : System.getProperty(ENVIRONMENT_VARIABLE_NAME);
return (applicationName != null) ? applicationName : "";
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
@ -16,6 +16,7 @@
package org.springframework.boot.logging.logback;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -30,9 +31,9 @@ import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
/**
* Logback {@link CompositeConverter} colors output using the {@link AnsiOutput} class. A
* single 'color' option can be provided to the converter, or if not specified color will
* be picked based on the logging level.
* Logback {@link CompositeConverter} to color output using the {@link AnsiOutput} class.
* A single 'color' option can be provided to the converter, or if not specified color
* will be picked based on the logging level.
*
* @author Phillip Webb
* @since 1.0.0
@ -43,23 +44,10 @@ public class ColorConverter extends CompositeConverter<ILoggingEvent> {
static {
Map<String, AnsiElement> ansiElements = new HashMap<>();
ansiElements.put("black", AnsiColor.BLACK);
ansiElements.put("white", AnsiColor.WHITE);
Arrays.stream(AnsiColor.values())
.filter((color) -> color != AnsiColor.DEFAULT)
.forEach((color) -> ansiElements.put(color.name().toLowerCase(), color));
ansiElements.put("faint", AnsiStyle.FAINT);
ansiElements.put("red", AnsiColor.RED);
ansiElements.put("green", AnsiColor.GREEN);
ansiElements.put("yellow", AnsiColor.YELLOW);
ansiElements.put("blue", AnsiColor.BLUE);
ansiElements.put("magenta", AnsiColor.MAGENTA);
ansiElements.put("cyan", AnsiColor.CYAN);
ansiElements.put("bright_black", AnsiColor.BRIGHT_BLACK);
ansiElements.put("bright_white", AnsiColor.BRIGHT_WHITE);
ansiElements.put("bright_red", AnsiColor.BRIGHT_RED);
ansiElements.put("bright_green", AnsiColor.BRIGHT_GREEN);
ansiElements.put("bright_yellow", AnsiColor.BRIGHT_YELLOW);
ansiElements.put("bright_blue", AnsiColor.BRIGHT_BLUE);
ansiElements.put("bright_magenta", AnsiColor.BRIGHT_MAGENTA);
ansiElements.put("bright_cyan", AnsiColor.BRIGHT_CYAN);
ELEMENTS = Collections.unmodifiableMap(ansiElements);
}
@ -74,17 +62,26 @@ public class ColorConverter extends CompositeConverter<ILoggingEvent> {
@Override
protected String transform(ILoggingEvent event, String in) {
AnsiElement element = ELEMENTS.get(getFirstOption());
if (element == null) {
AnsiElement color = ELEMENTS.get(getFirstOption());
if (color == null) {
// Assume highlighting
element = LEVELS.get(event.getLevel().toInteger());
element = (element != null) ? element : AnsiColor.GREEN;
color = LEVELS.get(event.getLevel().toInteger());
color = (color != null) ? color : AnsiColor.GREEN;
}
return toAnsiString(in, element);
return toAnsiString(in, color);
}
protected String toAnsiString(String in, AnsiElement element) {
return AnsiOutput.toString(element, in);
}
static String getName(AnsiElement element) {
return ELEMENTS.entrySet()
.stream()
.filter((entry) -> entry.getValue().equals(element))
.map(Map.Entry::getKey)
.findFirst()
.orElseThrow();
}
}

View File

@ -30,6 +30,9 @@ import ch.qos.logback.core.spi.ScanException;
import ch.qos.logback.core.util.FileSize;
import ch.qos.logback.core.util.OptionHelper;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiElement;
import org.springframework.boot.ansi.AnsiStyle;
import org.springframework.boot.logging.LogFile;
/**
@ -47,6 +50,25 @@ import org.springframework.boot.logging.LogFile;
*/
class DefaultLogbackConfiguration {
private static String DEFAULT_CHARSET = Charset.defaultCharset().name();
private static String NAME_AND_GROUP = "%applicationName%applicationGroup";
private static String DATETIME = "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}";
private static String DEFAULT_CONSOLE_LOG_PATTERN = faint(DATETIME) + " "
+ colorByLevel("${LOG_LEVEL_PATTERN:-%5p}") + " " + magenta("${PID:-}") + " "
+ faint("--- " + NAME_AND_GROUP + "[%15.15t] ${LOG_CORRELATION_PATTERN:-}") + cyan("%-40.40logger{39}")
+ " " + faint(":") + " %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}";
static final String CONSOLE_LOG_PATTERN = "${CONSOLE_LOG_PATTERN:-" + DEFAULT_CONSOLE_LOG_PATTERN;
private static String DEFAULT_FILE_LOG_PATTERN = DATETIME + " ${LOG_LEVEL_PATTERN:-%5p} ${PID:-} --- "
+ NAME_AND_GROUP + "[%t] ${LOG_CORRELATION_PATTERN:-}"
+ "%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}";
static final String FILE_LOG_PATTERN = "${FILE_LOG_PATTERN:-" + DEFAULT_FILE_LOG_PATTERN;
private final LogFile logFile;
DefaultLogbackConfiguration(LogFile logFile) {
@ -74,24 +96,12 @@ class DefaultLogbackConfiguration {
config.conversionRule("correlationId", CorrelationIdConverter.class);
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class);
config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class);
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%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();
config.getContext()
.putProperty("CONSOLE_LOG_CHARSET", resolve(config, "${CONSOLE_LOG_CHARSET:-" + defaultCharset + "}"));
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%applicationGroup[%t] "
+ "${LOG_CORRELATION_PATTERN:-}"
+ "%-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"));
config.getContext()
.putProperty("FILE_LOG_CHARSET", resolve(config, "${FILE_LOG_CHARSET:-" + defaultCharset + "}"));
config.getContext().putProperty("FILE_LOG_THRESHOLD", resolve(config, "${FILE_LOG_THRESHOLD:-TRACE}"));
putProperty(config, "CONSOLE_LOG_PATTERN", CONSOLE_LOG_PATTERN);
putProperty(config, "CONSOLE_LOG_CHARSET", "${CONSOLE_LOG_CHARSET:-" + DEFAULT_CHARSET + "}");
putProperty(config, "CONSOLE_LOG_THRESHOLD", "${CONSOLE_LOG_THRESHOLD:-TRACE}");
putProperty(config, "FILE_LOG_PATTERN", FILE_LOG_PATTERN);
putProperty(config, "FILE_LOG_CHARSET", "${FILE_LOG_CHARSET:-" + DEFAULT_CHARSET + "}");
putProperty(config, "FILE_LOG_THRESHOLD", "${FILE_LOG_THRESHOLD:-TRACE}");
config.logger("org.apache.catalina.startup.DigesterFactory", Level.ERROR);
config.logger("org.apache.catalina.util.LifecycleBase", Level.ERROR);
config.logger("org.apache.coyote.http11.Http11NioProtocol", Level.WARN);
@ -99,7 +109,11 @@ class DefaultLogbackConfiguration {
config.logger("org.apache.tomcat.util.net.NioSelectorPool", Level.WARN);
config.logger("org.eclipse.jetty.util.component.AbstractLifeCycle", Level.ERROR);
config.logger("org.hibernate.validator.internal.util.Version", Level.WARN);
config.logger("org.springframework.boot.actuate.endpoint.jmx", Level.WARN);
config.logger("org.springframework.boot.actuate.endpoint.jmx", Level.WARN);// @formatter:on
}
void putProperty(LogbackConfigurator config, String name, String val) {
config.getContext().putProperty(name, resolve(config, val));
}
private Appender<ILoggingEvent> consoleAppender(LogbackConfigurator config) {
@ -174,4 +188,24 @@ class DefaultLogbackConfiguration {
}
}
private static String faint(String value) {
return color(value, AnsiStyle.FAINT);
}
private static String cyan(String value) {
return color(value, AnsiColor.CYAN);
}
private static String magenta(String value) {
return color(value, AnsiColor.MAGENTA);
}
private static String colorByLevel(String value) {
return "%clr(" + value + "){}";
}
private static String color(String value, AnsiElement ansiElement) {
return "%clr(" + value + "){" + ColorConverter.getName(ansiElement) + "}";
}
}

View File

@ -177,7 +177,7 @@
"type": "java.lang.String",
"description": "Appender pattern for output to the console. Supported only with the default Logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": "%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([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
"defaultValue": "%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([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
},
{
"name": "logging.pattern.correlation",
@ -197,7 +197,7 @@
"type": "java.lang.String",
"description": "Appender pattern for output to a file. Supported only with the default Logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
"defaultValue": "%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:-} --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"
},
{
"name": "logging.pattern.level",

View File

@ -4,7 +4,7 @@
<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:-}${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="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>
<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>

View File

@ -4,7 +4,7 @@
<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:-}${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="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>
<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>

View File

@ -12,10 +12,10 @@ Default logback configuration provided for import
<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%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_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%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%applicationGroup[%t] ${LOG_CORRELATION_PATTERN:-}%-40.40logger{39} : %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(--- %applicationName%applicationGroup[%15.15t] ${LOG_CORRELATION_PATTERN:-}){faint}%clr(%-40.40logger{39}){cyan} %clr(:){faint} %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}"/>

View File

@ -159,20 +159,20 @@ class LoggingSystemPropertiesTests {
@Test
void loggedApplicationGroupWhenHasApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment().withProperty("spring.application.group", "test")).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP)).isEqualTo("[test] ");
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isEqualTo("[test] ");
}
@Test
void loggedApplicationGroupWhenHasNoApplicationGroup() {
new LoggingSystemProperties(new MockEnvironment()).apply(null);
assertThat(getSystemProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP)).isNull();
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.LOGGED_APPLICATION_GROUP)).isNull();
assertThat(getSystemProperty(LoggingSystemProperty.APPLICATION_GROUP)).isNull();
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* 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.
@ -53,10 +53,6 @@ class ColorConverterTests {
this.event = new TestLogEvent();
}
private ColorConverter newConverter(String styling) {
return ColorConverter.newInstance(null, new String[] { this.in, styling });
}
@Test
void black() {
StringBuilder output = new StringBuilder();
@ -216,6 +212,10 @@ class ColorConverterTests {
assertThat(output).hasToString("\033[32min\033[0;39m");
}
private ColorConverter newConverter(String styling) {
return ColorConverter.newInstance(null, new String[] { this.in, styling });
}
static class TestLogEvent extends AbstractLogEvent {
private Level level;

View File

@ -65,16 +65,16 @@ class ApplicationGroupConverterTests {
private void withLoggedApplicationGroup(String group, Runnable action) {
if (group == null) {
System.clearProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName());
System.clearProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
}
else {
System.setProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName(), group);
System.setProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName(), group);
}
try {
action.run();
}
finally {
System.clearProperty(LoggingSystemProperty.LOGGED_APPLICATION_GROUP.getEnvironmentVariableName());
System.clearProperty(LoggingSystemProperty.APPLICATION_GROUP.getEnvironmentVariableName());
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DefaultLogbackConfiguration}
*
* @author Phillip Webb
*/
class DefaultLogbackConfigurationTests {
@Test
void defaultLogbackXmlContainsConsoleLogPattern() throws Exception {
assertThatDefaultXmlContains("CONSOLE_LOG_PATTERN", DefaultLogbackConfiguration.CONSOLE_LOG_PATTERN);
}
@Test
void defaultLogbackXmlContainsFileLogPattern() throws Exception {
assertThatDefaultXmlContains("FILE_LOG_PATTERN", DefaultLogbackConfiguration.FILE_LOG_PATTERN);
}
private void assertThatDefaultXmlContains(String name, String value) throws Exception {
String expected = "<property name=\"%s\" value=\"%s\"/>".formatted(name, value);
assertThat(defaultXmlContent()).contains(expected);
}
private String defaultXmlContent() throws IOException {
return StreamUtils.copyToString(getClass().getResourceAsStream("defaults.xml"), StandardCharsets.UTF_8);
}
}