diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java index 023ffb6c4ad..0249fd261ba 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java @@ -87,7 +87,7 @@ record StructuredLoggingJsonProperties(Set include, Set exclude, Boolean includeCommonFrames, Boolean includeHashes) { StackTracePrinter createPrinter() { - String name = getPrinter(); + String name = sanitizePrinter(); if ("loggingsystem".equals(name) || (name.isEmpty() && !hasAnyOtherProperty())) { return null; } @@ -101,14 +101,14 @@ record StructuredLoggingJsonProperties(Set include, Set exclude, } boolean hasCustomPrinter() { - String name = getPrinter(); + String name = sanitizePrinter(); if (name.isEmpty()) { return false; } return !("loggingsystem".equals(name) || "standard".equals(name)); } - private String getPrinter() { + private String sanitizePrinter() { return Objects.toString(printer(), "").toLowerCase(Locale.ROOT).replace("-", ""); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor.java index fc48e89b7ee..946fc2c6116 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor.java @@ -32,7 +32,7 @@ import org.springframework.core.env.Environment; /** * {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for - * {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}. + * {@link StructuredLoggingJsonProperties}. * * @author Dmytro Nosan * @author Yanming Zhou @@ -47,17 +47,34 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class); StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment); - return (properties != null) ? AotContribution.get(properties) : null; + if (properties != null) { + Set>> customizers = properties.customizer(); + String stackTracePrinter = getCustomStackTracePrinter(properties); + if (stackTracePrinter != null || !customizers.isEmpty()) { + return new AotContribution(beanFactory.getBeanClassLoader(), customizers, stackTracePrinter); + } + } + return null; + } + + private static String getCustomStackTracePrinter(StructuredLoggingJsonProperties properties) { + return Optional.ofNullable(properties.stackTrace()) + .filter(StackTrace::hasCustomPrinter) + .map(StackTrace::printer) + .orElse(null); } private static final class AotContribution implements BeanFactoryInitializationAotContribution { + private final ClassLoader classLoader; + private final Set>> customizers; private final String stackTracePrinter; - private AotContribution(Set>> customizers, - String stackTracePrinter) { + private AotContribution(ClassLoader classLoader, + Set>> customizers, String stackTracePrinter) { + this.classLoader = classLoader; this.customizers = customizers; this.stackTracePrinter = stackTracePrinter; } @@ -69,27 +86,11 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor this.customizers.forEach((customizer) -> reflection.registerType(customizer, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); if (this.stackTracePrinter != null) { - reflection.registerTypeIfPresent(getClass().getClassLoader(), this.stackTracePrinter, + reflection.registerTypeIfPresent(this.classLoader, this.stackTracePrinter, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS); } } - static AotContribution get(StructuredLoggingJsonProperties properties) { - Set>> customizers = properties.customizer(); - String stackTracePrinter = getStackTracePrinter(properties); - if (stackTracePrinter == null && customizers.isEmpty()) { - return null; - } - return new AotContribution(customizers, stackTracePrinter); - } - - private static String getStackTracePrinter(StructuredLoggingJsonProperties properties) { - return Optional.ofNullable(properties.stackTrace()) - .filter(StackTrace::hasCustomPrinter) - .map(StackTrace::printer) - .orElse(null); - } - } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests.java index 3cf88741d5c..7ba7d4d5425 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests.java @@ -67,7 +67,7 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests } @Test - void shouldRegisterRuntimeHintsWhenStackTracePrinterIsPresent() { + void shouldRegisterRuntimeHintsWhenCustomStackTracePrinterIsPresent() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.structured.json.stacktrace.printer", TestStackTracePrinter.class.getName());