Skip to content

Commit 68022ef

Browse files
committed
Use Class reference rather than String for customizer
Update `StructuredLoggingJsonProperties` to use a real Class reference rather than a String. Closes spring-projectsgh-43202
1 parent d83d34d commit 68022ef

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @author Phillip Webb
3535
*/
3636
record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude, Map<String, String> rename,
37-
Map<String, String> add, String customizer) {
37+
Map<String, String> add, Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer) {
3838

3939
static StructuredLoggingJsonProperties get(Environment environment) {
4040
return Binder.get(environment)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.boot.json.JsonWriter.Members;
2323
import org.springframework.boot.util.Instantiator;
2424
import org.springframework.util.CollectionUtils;
25-
import org.springframework.util.StringUtils;
2625

2726
/**
2827
* {@link StructureLoggingJsonMembersCustomizer} to apply
@@ -50,8 +49,8 @@ public void customize(Members<Object> members) {
5049
if (!CollectionUtils.isEmpty(add)) {
5150
add.forEach(members::add);
5251
}
53-
String customizer = this.properties.customizer();
54-
if (StringUtils.hasLength(customizer)) {
52+
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer = this.properties.customizer();
53+
if (customizer != null) {
5554
createAndApplyCustomizer(members, customizer);
5655
}
5756
}
@@ -71,8 +70,9 @@ boolean filterPath(MemberPath path) {
7170
}
7271

7372
@SuppressWarnings({ "unchecked", "rawtypes" })
74-
private void createAndApplyCustomizer(Members<Object> members, String customizerClassName) {
75-
((StructureLoggingJsonMembersCustomizer) this.instantiator.instantiate(customizerClassName)).customize(members);
73+
private void createAndApplyCustomizer(Members<Object> members,
74+
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizerClass) {
75+
((StructureLoggingJsonMembersCustomizer) this.instantiator.instantiateType(customizerClass)).customize(members);
7676
}
7777

7878
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/Instantiator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ public T instantiate(ClassLoader classLoader, String name) {
148148
return instantiate(TypeSupplier.forName(classLoader, name));
149149
}
150150

151+
/**
152+
* Instantiate the given set of classes, injecting constructor arguments as necessary.
153+
* @param type the types to instantiate
154+
* @return a list of instantiated instances
155+
* @since 3.4.0
156+
*/
157+
public T instantiateType(Class<?> type) {
158+
Assert.notNull(type, "Type must not be null");
159+
return instantiate(TypeSupplier.forType(type));
160+
}
161+
151162
/**
152163
* Instantiate the given set of classes, injecting constructor arguments as necessary.
153164
* @param types the types to instantiate

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.mockito.junit.jupiter.MockitoExtension;
2727

2828
import org.springframework.boot.json.JsonWriter;
29+
import org.springframework.boot.json.JsonWriter.Members;
2930
import org.springframework.boot.json.JsonWriter.NameProcessor;
3031
import org.springframework.boot.util.Instantiator;
3132

@@ -95,13 +96,13 @@ void customizeWhenHasAddAddsMemeber() {
9596
}
9697

9798
@Test
98-
@SuppressWarnings("rawtypes")
99+
@SuppressWarnings({ "rawtypes", "unchecked" })
99100
void customizeWhenHasCustomizerCustomizesMember() {
100101
StructureLoggingJsonMembersCustomizer<?> uppercaseCustomizer = (members) -> members
101102
.applyingNameProcessor(NameProcessor.of(String::toUpperCase));
102-
given(((Instantiator) this.instantiator).instantiate("test")).willReturn(uppercaseCustomizer);
103+
given(((Instantiator) this.instantiator).instantiateType(TestCustomizer.class)).willReturn(uppercaseCustomizer);
103104
StructuredLoggingJsonProperties properties = new StructuredLoggingJsonProperties(Collections.emptySet(),
104-
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), "test");
105+
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), TestCustomizer.class);
105106
StructuredLoggingJsonPropertiesJsonMembersCustomizer customizer = new StructuredLoggingJsonPropertiesJsonMembersCustomizer(
106107
this.instantiator, properties);
107108
assertThat(writeSampleJson(customizer)).contains("\"A\":\"a\"");
@@ -117,4 +118,12 @@ private String writeSampleJson(StructureLoggingJsonMembersCustomizer customizer)
117118
}).writeToString(new Object());
118119
}
119120

121+
static class TestCustomizer implements StructureLoggingJsonMembersCustomizer<String> {
122+
123+
@Override
124+
public void customize(Members<String> members) {
125+
}
126+
127+
}
128+
120129
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.boot.json.JsonWriter.Members;
2425
import org.springframework.mock.env.MockEnvironment;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,10 +40,10 @@ void getBindsFromEnvironment() {
3940
environment.setProperty("logging.structured.json.exclude", "c,d");
4041
environment.setProperty("logging.structured.json.rename.e", "f");
4142
environment.setProperty("logging.structured.json.add.g", "h");
42-
environment.setProperty("logging.structured.json.customizer", "i");
43+
environment.setProperty("logging.structured.json.customizer", TestCustomizer.class.getName());
4344
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
4445
assertThat(properties).isEqualTo(new StructuredLoggingJsonProperties(Set.of("a", "b"), Set.of("c", "d"),
45-
Map.of("e", "f"), Map.of("g", "h"), "i"));
46+
Map.of("e", "f"), Map.of("g", "h"), TestCustomizer.class));
4647
}
4748

4849
@Test
@@ -51,4 +52,12 @@ void getWhenNoBoundPropertiesReturnsNull() {
5152
StructuredLoggingJsonProperties.get(environment);
5253
}
5354

55+
static class TestCustomizer implements StructureLoggingJsonMembersCustomizer<String> {
56+
57+
@Override
58+
public void customize(Members<String> members) {
59+
}
60+
61+
}
62+
5463
}

0 commit comments

Comments
 (0)