From db5830a2e0515318170b7fc6836bd1ca092a5d7d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 10 Jul 2024 15:15:29 -0700 Subject: [PATCH] Polish logging related code --- .../boot/logging/LoggingSystemProperties.java | 2 +- .../boot/logging/LoggingSystemProperty.java | 4 +- .../boot/logging/log4j2/ColorConverter.java | 66 +++++++---------- .../logback/ApplicationGroupConverter.java | 20 +++--- .../logback/ApplicationNameConverter.java | 17 ++--- .../boot/logging/logback/ColorConverter.java | 47 ++++++------ .../logback/DefaultLogbackConfiguration.java | 72 ++++++++++++++----- ...itional-spring-configuration-metadata.json | 4 +- .../boot/logging/log4j2/log4j2-file.xml | 2 +- .../boot/logging/log4j2/log4j2.xml | 2 +- .../boot/logging/logback/defaults.xml | 4 +- .../logging/LoggingSystemPropertiesTests.java | 6 +- .../logging/log4j2/ColorConverterTests.java | 10 +-- .../ApplicationGroupConverterTests.java | 6 +- .../DefaultLogbackConfigurationTests.java | 54 ++++++++++++++ 15 files changed, 191 insertions(+), 125 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/DefaultLogbackConfigurationTests.java 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 a339bd860d1..e88aeaab891 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 @@ -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)); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java index cf8e5dde47f..3ff65faf7c8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperty.java @@ -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. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java index 86abf0eb81b..64fcebd57c4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/ColorConverter.java @@ -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 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 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 formatters = parser.parse(options[0]); + AnsiElement element = (options.length != 1) ? ELEMENTS.get(options[1]) : null; + return new ColorConverter(formatters, element); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationGroupConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationGroupConverter.java index 21d8caca4fe..a2864378fe3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationGroupConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationGroupConverter.java @@ -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 : ""; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationNameConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationNameConverter.java index cc893605c71..2a2ea5fe4f6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationNameConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ApplicationNameConverter.java @@ -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 : ""; } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ColorConverter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ColorConverter.java index 7b1890257a7..b137423374a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ColorConverter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/ColorConverter.java @@ -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 { static { Map 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 { @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(); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 0feee7bbfce..608a3e0c349 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -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 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) + "}"; + } + } diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 7449773a9a9..424434744f0 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml index 094c991b2c4..83aa5cbdc14 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml @@ -4,7 +4,7 @@ %xwEx %5p yyyy-MM-dd'T'HH:mm:ss.SSSXXX - %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} + %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} %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} diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml index 0e36758fbcd..d91df802fea 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml @@ -4,7 +4,7 @@ %xwEx %5p yyyy-MM-dd'T'HH:mm:ss.SSSXXX - %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} + %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} %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} diff --git a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml index c4f46811118..47c11101b3d 100644 --- a/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml +++ b/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml @@ -12,10 +12,10 @@ Default logback configuration provided for import - + - + 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 c2d94811899..3f81feaeeca 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 @@ -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 diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/ColorConverterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/ColorConverterTests.java index 433c57ff157..aa8c5f0dfcb 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/ColorConverterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/ColorConverterTests.java @@ -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; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/ApplicationGroupConverterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/ApplicationGroupConverterTests.java index cc3bad8a52d..5375814301f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/ApplicationGroupConverterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/ApplicationGroupConverterTests.java @@ -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()); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/DefaultLogbackConfigurationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/DefaultLogbackConfigurationTests.java new file mode 100644 index 00000000000..5d5431fa022 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/DefaultLogbackConfigurationTests.java @@ -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 = "".formatted(name, value); + assertThat(defaultXmlContent()).contains(expected); + } + + private String defaultXmlContent() throws IOException { + return StreamUtils.copyToString(getClass().getResourceAsStream("defaults.xml"), StandardCharsets.UTF_8); + } + +}