-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Native image ignore structured logging fields
Add RuntimeHints for GraylogExtendedLogFormatProperties, StructuredLoggingJsonProperties and ElasticCommonSchemaProperties properties. Add BeanFactoryInitializationAotProcessor to register RuntimeHints for a custom StructuredLoggingJsonMembersCustomizer. See gh-43861 Signed-off-by: Dmytro Nosan <[email protected]>
- Loading branch information
Showing
9 changed files
with
273 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ructured/StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012-2025 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.structured; | ||
|
||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.core.env.Environment; | ||
|
||
/** | ||
* {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for | ||
* {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor | ||
implements BeanFactoryInitializationAotProcessor { | ||
|
||
private static final String ENVIRONMENT_BEAN_NAME = "environment"; | ||
|
||
@Override | ||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { | ||
Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class); | ||
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment); | ||
if (properties != null && properties.customizer() != null) { | ||
return new AotContribution(properties.customizer()); | ||
} | ||
return null; | ||
} | ||
|
||
private static final class AotContribution implements BeanFactoryInitializationAotContribution { | ||
|
||
private final Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer; | ||
|
||
private AotContribution(Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer) { | ||
this.customizer = customizer; | ||
} | ||
|
||
@Override | ||
public void applyTo(GenerationContext generationContext, | ||
BeanFactoryInitializationCode beanFactoryInitializationCode) { | ||
generationContext.getRuntimeHints() | ||
.reflection() | ||
.registerType(this.customizer, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, | ||
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...red/StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2012-2025 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.structured; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; | ||
import org.springframework.aot.test.generate.TestGenerationContext; | ||
import org.springframework.beans.factory.aot.AotServices; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; | ||
import org.springframework.boot.json.JsonWriter.Members; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for | ||
* {@link StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor}. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorTests { | ||
|
||
@Test | ||
void structuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorIsRegistered() { | ||
assertThat(AotServices.factories().load(BeanFactoryInitializationAotProcessor.class)) | ||
.anyMatch(StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor.class::isInstance); | ||
} | ||
|
||
@Test | ||
void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() { | ||
MockEnvironment environment = new MockEnvironment(); | ||
environment.setProperty("logging.structured.json.customizer", TestCustomizer.class.getName()); | ||
|
||
BeanFactoryInitializationAotContribution contribution = getContribution(environment); | ||
assertThat(contribution).isNotNull(); | ||
|
||
RuntimeHints hints = getRuntimeHints(contribution); | ||
assertThat(RuntimeHintsPredicates.reflection() | ||
.onType(TestCustomizer.class) | ||
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, | ||
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)) | ||
.accepts(hints); | ||
} | ||
|
||
@Test | ||
void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() { | ||
MockEnvironment environment = new MockEnvironment(); | ||
BeanFactoryInitializationAotContribution contribution = getContribution(environment); | ||
assertThat(contribution).isNull(); | ||
} | ||
|
||
private BeanFactoryInitializationAotContribution getContribution(ConfigurableEnvironment environment) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { | ||
context.setEnvironment(environment); | ||
context.refresh(); | ||
return new StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor() | ||
.processAheadOfTime(context.getBeanFactory()); | ||
} | ||
} | ||
|
||
private RuntimeHints getRuntimeHints(BeanFactoryInitializationAotContribution contribution) { | ||
TestGenerationContext generationContext = new TestGenerationContext(); | ||
contribution.applyTo(generationContext, null); | ||
return generationContext.getRuntimeHints(); | ||
} | ||
|
||
static class TestCustomizer implements StructuredLoggingJsonMembersCustomizer<String> { | ||
|
||
@Override | ||
public void customize(Members<String> members) { | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.