Polish "Add RuntimeHints for StackTracePrinter"
See gh-44242
This commit is contained in:
parent
5d781ffcbb
commit
f866e20410
|
@ -87,7 +87,7 @@ record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude,
|
||||||
Boolean includeCommonFrames, Boolean includeHashes) {
|
Boolean includeCommonFrames, Boolean includeHashes) {
|
||||||
|
|
||||||
StackTracePrinter createPrinter() {
|
StackTracePrinter createPrinter() {
|
||||||
String name = getPrinter();
|
String name = sanitizePrinter();
|
||||||
if ("loggingsystem".equals(name) || (name.isEmpty() && !hasAnyOtherProperty())) {
|
if ("loggingsystem".equals(name) || (name.isEmpty() && !hasAnyOtherProperty())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -101,14 +101,14 @@ record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude,
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean hasCustomPrinter() {
|
boolean hasCustomPrinter() {
|
||||||
String name = getPrinter();
|
String name = sanitizePrinter();
|
||||||
if (name.isEmpty()) {
|
if (name.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return !("loggingsystem".equals(name) || "standard".equals(name));
|
return !("loggingsystem".equals(name) || "standard".equals(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getPrinter() {
|
private String sanitizePrinter() {
|
||||||
return Objects.toString(printer(), "").toLowerCase(Locale.ROOT).replace("-", "");
|
return Objects.toString(printer(), "").toLowerCase(Locale.ROOT).replace("-", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for
|
* {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for
|
||||||
* {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}.
|
* {@link StructuredLoggingJsonProperties}.
|
||||||
*
|
*
|
||||||
* @author Dmytro Nosan
|
* @author Dmytro Nosan
|
||||||
* @author Yanming Zhou
|
* @author Yanming Zhou
|
||||||
|
@ -47,17 +47,34 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor
|
||||||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
|
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
|
||||||
Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
|
Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
|
||||||
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
|
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
|
||||||
return (properties != null) ? AotContribution.get(properties) : null;
|
if (properties != null) {
|
||||||
|
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> 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 static final class AotContribution implements BeanFactoryInitializationAotContribution {
|
||||||
|
|
||||||
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
private final Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers;
|
private final Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers;
|
||||||
|
|
||||||
private final String stackTracePrinter;
|
private final String stackTracePrinter;
|
||||||
|
|
||||||
private AotContribution(Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers,
|
private AotContribution(ClassLoader classLoader,
|
||||||
String stackTracePrinter) {
|
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizers, String stackTracePrinter) {
|
||||||
|
this.classLoader = classLoader;
|
||||||
this.customizers = customizers;
|
this.customizers = customizers;
|
||||||
this.stackTracePrinter = stackTracePrinter;
|
this.stackTracePrinter = stackTracePrinter;
|
||||||
}
|
}
|
||||||
|
@ -69,27 +86,11 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessor
|
||||||
this.customizers.forEach((customizer) -> reflection.registerType(customizer,
|
this.customizers.forEach((customizer) -> reflection.registerType(customizer,
|
||||||
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
|
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
|
||||||
if (this.stackTracePrinter != null) {
|
if (this.stackTracePrinter != null) {
|
||||||
reflection.registerTypeIfPresent(getClass().getClassLoader(), this.stackTracePrinter,
|
reflection.registerTypeIfPresent(this.classLoader, this.stackTracePrinter,
|
||||||
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
|
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static AotContribution get(StructuredLoggingJsonProperties properties) {
|
|
||||||
Set<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ class StructuredLoggingJsonPropertiesBeanFactoryInitializationAotProcessorTests
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldRegisterRuntimeHintsWhenStackTracePrinterIsPresent() {
|
void shouldRegisterRuntimeHintsWhenCustomStackTracePrinterIsPresent() {
|
||||||
MockEnvironment environment = new MockEnvironment();
|
MockEnvironment environment = new MockEnvironment();
|
||||||
environment.setProperty("logging.structured.json.stacktrace.printer", TestStackTracePrinter.class.getName());
|
environment.setProperty("logging.structured.json.stacktrace.printer", TestStackTracePrinter.class.getName());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue