Skip to content

Commit 77a735f

Browse files
committed
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 <dimanosan@gmail.com>
1 parent e48cfce commit 77a735f

9 files changed

+281
-7
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/ElasticCommonSchemaProperties.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging.structured;
1818

19+
import org.springframework.boot.context.properties.bind.BindableRuntimeHintsRegistrar;
1920
import org.springframework.boot.context.properties.bind.Binder;
2021
import org.springframework.boot.json.JsonWriter;
2122
import org.springframework.boot.json.JsonWriter.Members;
@@ -91,4 +92,12 @@ Service withDefaults(Environment environment) {
9192

9293
}
9394

95+
static class ElasticCommonSchemaPropertiesRuntimeHints extends BindableRuntimeHintsRegistrar {
96+
97+
ElasticCommonSchemaPropertiesRuntimeHints() {
98+
super(ElasticCommonSchemaProperties.class);
99+
}
100+
101+
}
102+
94103
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatProperties.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging.structured;
1818

19+
import org.springframework.boot.context.properties.bind.BindableRuntimeHintsRegistrar;
1920
import org.springframework.boot.context.properties.bind.Binder;
2021
import org.springframework.boot.json.JsonWriter;
2122
import org.springframework.core.env.Environment;
@@ -86,4 +87,12 @@ void jsonMembers(JsonWriter.Members<?> members) {
8687

8788
}
8889

90+
static class GraylogExtendedLogFormatPropertiesRuntimeHints extends BindableRuntimeHintsRegistrar {
91+
92+
GraylogExtendedLogFormatPropertiesRuntimeHints() {
93+
super(GraylogExtendedLogFormatProperties.class);
94+
}
95+
96+
}
97+
8998
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.structured;
18+
19+
import org.springframework.aot.generate.GenerationContext;
20+
import org.springframework.aot.hint.MemberCategory;
21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
23+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
24+
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
25+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26+
import org.springframework.core.env.Environment;
27+
28+
/**
29+
* {@link BeanFactoryInitializationAotProcessor} that registers {@link RuntimeHints} for
30+
* {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}.
31+
*
32+
* @author Dmytro Nosan
33+
*/
34+
class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor
35+
implements BeanFactoryInitializationAotProcessor {
36+
37+
private static final String ENVIRONMENT_BEAN_NAME = "environment";
38+
39+
@Override
40+
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
41+
Environment environment = beanFactory.getBean(ENVIRONMENT_BEAN_NAME, Environment.class);
42+
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
43+
if (properties != null && properties.customizer() != null) {
44+
return new AotContribution(properties.customizer());
45+
}
46+
return null;
47+
}
48+
49+
private static final class AotContribution implements BeanFactoryInitializationAotContribution {
50+
51+
private final Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer;
52+
53+
private AotContribution(Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer) {
54+
this.customizer = customizer;
55+
}
56+
57+
@Override
58+
public void applyTo(GenerationContext generationContext,
59+
BeanFactoryInitializationCode beanFactoryInitializationCode) {
60+
generationContext.getRuntimeHints()
61+
.reflection()
62+
.registerType(this.customizer, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
63+
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
64+
}
65+
66+
}
67+
68+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/structured/StructuredLoggingJsonProperties.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.Set;
2121

22+
import org.springframework.boot.context.properties.bind.BindableRuntimeHintsRegistrar;
2223
import org.springframework.boot.context.properties.bind.Binder;
2324
import org.springframework.core.env.Environment;
2425

@@ -42,4 +43,12 @@ static StructuredLoggingJsonProperties get(Environment environment) {
4243
.orElse(null);
4344
}
4445

46+
static class StructuredLoggingJsonPropertiesRuntimeHints extends BindableRuntimeHintsRegistrar {
47+
48+
StructuredLoggingJsonPropertiesRuntimeHints() {
49+
super(StructuredLoggingJsonProperties.class);
50+
}
51+
52+
}
53+
4554
}

spring-boot-project/spring-boot/src/main/resources/META-INF/spring/aot.factories

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ org.springframework.boot.jdbc.DataSourceBuilderRuntimeHints,\
1010
org.springframework.boot.json.JacksonRuntimeHints,\
1111
org.springframework.boot.logging.java.JavaLoggingSystemRuntimeHints,\
1212
org.springframework.boot.logging.logback.LogbackRuntimeHints,\
13+
org.springframework.boot.logging.structured.ElasticCommonSchemaProperties.ElasticCommonSchemaPropertiesRuntimeHints,\
14+
org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties.GraylogExtendedLogFormatPropertiesRuntimeHints,\
15+
org.springframework.boot.logging.structured.StructuredLoggingJsonProperties.StructuredLoggingJsonPropertiesRuntimeHints,\
1316
org.springframework.boot.web.embedded.undertow.UndertowWebServer.UndertowWebServerRuntimeHints,\
1417
org.springframework.boot.web.server.MimeMappings.MimeMappingsRuntimeHints
1518

1619
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\
1720
org.springframework.boot.context.properties.ConfigurationPropertiesBeanFactoryInitializationAotProcessor,\
1821
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.EnvironmentBeanFactoryInitializationAotProcessor,\
19-
org.springframework.boot.jackson.JsonComponentModule.JsonComponentBeanFactoryInitializationAotProcessor
22+
org.springframework.boot.jackson.JsonComponentModule.JsonComponentBeanFactoryInitializationAotProcessor,\
23+
org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor
2024

2125
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
2226
org.springframework.boot.context.properties.ConfigurationPropertiesBeanRegistrationAotProcessor,\

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/ElasticCommonSchemaPropertiesTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,12 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.beans.factory.aot.AotServices;
2125
import org.springframework.boot.json.JsonWriter;
26+
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties.ElasticCommonSchemaPropertiesRuntimeHints;
2227
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties.Service;
2328
import org.springframework.mock.env.MockEnvironment;
2429

@@ -77,4 +82,24 @@ void addToJsonMembersCreatesValidJson() {
7782
+ "\"service.environment\":\"prod\",\"service.node.name\":\"boot\"}");
7883
}
7984

85+
@Test
86+
void shouldRegisterRuntimeHints() throws Exception {
87+
RuntimeHints hints = new RuntimeHints();
88+
new ElasticCommonSchemaPropertiesRuntimeHints().registerHints(hints, getClass().getClassLoader());
89+
assertThat(RuntimeHintsPredicates.reflection().onType(ElasticCommonSchemaProperties.class)).accepts(hints);
90+
assertThat(RuntimeHintsPredicates.reflection()
91+
.onConstructor(ElasticCommonSchemaProperties.class.getConstructor(Service.class))
92+
.invoke()).accepts(hints);
93+
assertThat(RuntimeHintsPredicates.reflection().onType(Service.class)).accepts(hints);
94+
assertThat(RuntimeHintsPredicates.reflection()
95+
.onConstructor(Service.class.getConstructor(String.class, String.class, String.class, String.class))
96+
.invoke()).accepts(hints);
97+
}
98+
99+
@Test
100+
void elasticCommonSchemaPropertiesRuntimeHintsIsRegistered() {
101+
assertThat(AotServices.factories().load(RuntimeHintsRegistrar.class))
102+
.anyMatch(ElasticCommonSchemaPropertiesRuntimeHints.class::isInstance);
103+
}
104+
80105
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/structured/GraylogExtendedLogFormatPropertiesTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,12 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.aot.hint.RuntimeHints;
22+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.beans.factory.aot.AotServices;
2125
import org.springframework.boot.json.JsonWriter;
26+
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties.GraylogExtendedLogFormatPropertiesRuntimeHints;
2227
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties.Service;
2328
import org.springframework.mock.env.MockEnvironment;
2429

@@ -72,4 +77,24 @@ void addToJsonMembersCreatesValidJson() {
7277
assertThat(writer.writeToString(properties)).isEqualTo("{\"host\":\"spring\",\"_service_version\":\"1.2.3\"}");
7378
}
7479

80+
@Test
81+
void shouldRegisterRuntimeHints() throws Exception {
82+
RuntimeHints hints = new RuntimeHints();
83+
new GraylogExtendedLogFormatPropertiesRuntimeHints().registerHints(hints, getClass().getClassLoader());
84+
assertThat(RuntimeHintsPredicates.reflection().onType(GraylogExtendedLogFormatProperties.class)).accepts(hints);
85+
assertThat(RuntimeHintsPredicates.reflection()
86+
.onConstructor(GraylogExtendedLogFormatProperties.class.getConstructor(String.class, Service.class))
87+
.invoke()).accepts(hints);
88+
assertThat(RuntimeHintsPredicates.reflection().onType(Service.class)).accepts(hints);
89+
assertThat(RuntimeHintsPredicates.reflection()
90+
.onConstructor(GraylogExtendedLogFormatProperties.Service.class.getConstructor(String.class))
91+
.invoke()).accepts(hints);
92+
}
93+
94+
@Test
95+
void graylogExtendedLogFormatPropertiesRuntimeHintsIsRegistered() {
96+
assertThat(AotServices.factories().load(RuntimeHintsRegistrar.class))
97+
.anyMatch(GraylogExtendedLogFormatPropertiesRuntimeHints.class::isInstance);
98+
}
99+
75100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.structured;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aot.hint.MemberCategory;
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
24+
import org.springframework.aot.test.generate.TestGenerationContext;
25+
import org.springframework.beans.factory.aot.AotServices;
26+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
27+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
28+
import org.springframework.boot.json.JsonWriter.Members;
29+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
30+
import org.springframework.core.env.ConfigurableEnvironment;
31+
import org.springframework.mock.env.MockEnvironment;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Tests for
37+
* {@link StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor}.
38+
*
39+
* @author Dmytro Nosan
40+
*/
41+
class StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorTests {
42+
43+
@Test
44+
void structuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessorIsRegistered() {
45+
assertThat(AotServices.factories().load(BeanFactoryInitializationAotProcessor.class))
46+
.anyMatch(StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor.class::isInstance);
47+
}
48+
49+
@Test
50+
void shouldRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() {
51+
MockEnvironment environment = new MockEnvironment();
52+
environment.setProperty("logging.structured.json.customizer", TestCustomizer.class.getName());
53+
54+
BeanFactoryInitializationAotContribution contribution = getContribution(environment);
55+
assertThat(contribution).isNotNull();
56+
57+
RuntimeHints hints = getRuntimeHints(contribution);
58+
assertThat(RuntimeHintsPredicates.reflection()
59+
.onType(TestCustomizer.class)
60+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
61+
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
62+
.accepts(hints);
63+
}
64+
65+
@Test
66+
void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHints() {
67+
MockEnvironment environment = new MockEnvironment();
68+
BeanFactoryInitializationAotContribution contribution = getContribution(environment);
69+
assertThat(contribution).isNull();
70+
}
71+
72+
@Test
73+
void shouldNotRegisterStructuredLoggingJsonMembersCustomizerRuntimeHintsWhenCustomizerIsNotSet() {
74+
MockEnvironment environment = new MockEnvironment();
75+
environment.setProperty("logging.structured.json.exclude", "something");
76+
BeanFactoryInitializationAotContribution contribution = getContribution(environment);
77+
assertThat(contribution).isNull();
78+
}
79+
80+
private BeanFactoryInitializationAotContribution getContribution(ConfigurableEnvironment environment) {
81+
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
82+
context.setEnvironment(environment);
83+
context.refresh();
84+
return new StructuredLoggingJsonMembersCustomizerBeanFactoryInitializationAotProcessor()
85+
.processAheadOfTime(context.getBeanFactory());
86+
}
87+
}
88+
89+
private RuntimeHints getRuntimeHints(BeanFactoryInitializationAotContribution contribution) {
90+
TestGenerationContext generationContext = new TestGenerationContext();
91+
contribution.applyTo(generationContext, null);
92+
return generationContext.getRuntimeHints();
93+
}
94+
95+
static class TestCustomizer implements StructuredLoggingJsonMembersCustomizer<String> {
96+
97+
@Override
98+
public void customize(Members<String> members) {
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)