Use Class reference rather than String for customizer
Update `StructuredLoggingJsonProperties` to use a real Class reference rather than a String. Closes gh-43202
This commit is contained in:
parent
d83d34d111
commit
68022ef0bb
|
@ -34,7 +34,7 @@ import org.springframework.core.env.Environment;
|
|||
* @author Phillip Webb
|
||||
*/
|
||||
record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude, Map<String, String> rename,
|
||||
Map<String, String> add, String customizer) {
|
||||
Map<String, String> add, Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer) {
|
||||
|
||||
static StructuredLoggingJsonProperties get(Environment environment) {
|
||||
return Binder.get(environment)
|
||||
|
|
|
@ -22,7 +22,6 @@ import org.springframework.boot.json.JsonWriter.MemberPath;
|
|||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.boot.util.Instantiator;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link StructureLoggingJsonMembersCustomizer} to apply
|
||||
|
@ -50,8 +49,8 @@ class StructuredLoggingJsonPropertiesJsonMembersCustomizer implements StructureL
|
|||
if (!CollectionUtils.isEmpty(add)) {
|
||||
add.forEach(members::add);
|
||||
}
|
||||
String customizer = this.properties.customizer();
|
||||
if (StringUtils.hasLength(customizer)) {
|
||||
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer = this.properties.customizer();
|
||||
if (customizer != null) {
|
||||
createAndApplyCustomizer(members, customizer);
|
||||
}
|
||||
}
|
||||
|
@ -71,8 +70,9 @@ class StructuredLoggingJsonPropertiesJsonMembersCustomizer implements StructureL
|
|||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
private void createAndApplyCustomizer(Members<Object> members, String customizerClassName) {
|
||||
((StructureLoggingJsonMembersCustomizer) this.instantiator.instantiate(customizerClassName)).customize(members);
|
||||
private void createAndApplyCustomizer(Members<Object> members,
|
||||
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizerClass) {
|
||||
((StructureLoggingJsonMembersCustomizer) this.instantiator.instantiateType(customizerClass)).customize(members);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -148,6 +148,17 @@ public class Instantiator<T> {
|
|||
return instantiate(TypeSupplier.forName(classLoader, name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate the given set of classes, injecting constructor arguments as necessary.
|
||||
* @param type the types to instantiate
|
||||
* @return a list of instantiated instances
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public T instantiateType(Class<?> type) {
|
||||
Assert.notNull(type, "Type must not be null");
|
||||
return instantiate(TypeSupplier.forType(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate the given set of classes, injecting constructor arguments as necessary.
|
||||
* @param types the types to instantiate
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.mockito.Mock;
|
|||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.boot.json.JsonWriter;
|
||||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.boot.json.JsonWriter.NameProcessor;
|
||||
import org.springframework.boot.util.Instantiator;
|
||||
|
||||
|
@ -95,13 +96,13 @@ class StructuredLoggingJsonPropertiesJsonMembersCustomizerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
void customizeWhenHasCustomizerCustomizesMember() {
|
||||
StructureLoggingJsonMembersCustomizer<?> uppercaseCustomizer = (members) -> members
|
||||
.applyingNameProcessor(NameProcessor.of(String::toUpperCase));
|
||||
given(((Instantiator) this.instantiator).instantiate("test")).willReturn(uppercaseCustomizer);
|
||||
given(((Instantiator) this.instantiator).instantiateType(TestCustomizer.class)).willReturn(uppercaseCustomizer);
|
||||
StructuredLoggingJsonProperties properties = new StructuredLoggingJsonProperties(Collections.emptySet(),
|
||||
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), "test");
|
||||
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), TestCustomizer.class);
|
||||
StructuredLoggingJsonPropertiesJsonMembersCustomizer customizer = new StructuredLoggingJsonPropertiesJsonMembersCustomizer(
|
||||
this.instantiator, properties);
|
||||
assertThat(writeSampleJson(customizer)).contains("\"A\":\"a\"");
|
||||
|
@ -117,4 +118,12 @@ class StructuredLoggingJsonPropertiesJsonMembersCustomizerTests {
|
|||
}).writeToString(new Object());
|
||||
}
|
||||
|
||||
static class TestCustomizer implements StructureLoggingJsonMembersCustomizer<String> {
|
||||
|
||||
@Override
|
||||
public void customize(Members<String> members) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.util.Set;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.json.JsonWriter.Members;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
@ -39,10 +40,10 @@ class StructuredLoggingJsonPropertiesTests {
|
|||
environment.setProperty("logging.structured.json.exclude", "c,d");
|
||||
environment.setProperty("logging.structured.json.rename.e", "f");
|
||||
environment.setProperty("logging.structured.json.add.g", "h");
|
||||
environment.setProperty("logging.structured.json.customizer", "i");
|
||||
environment.setProperty("logging.structured.json.customizer", TestCustomizer.class.getName());
|
||||
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
|
||||
assertThat(properties).isEqualTo(new StructuredLoggingJsonProperties(Set.of("a", "b"), Set.of("c", "d"),
|
||||
Map.of("e", "f"), Map.of("g", "h"), "i"));
|
||||
Map.of("e", "f"), Map.of("g", "h"), TestCustomizer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -51,4 +52,12 @@ class StructuredLoggingJsonPropertiesTests {
|
|||
StructuredLoggingJsonProperties.get(environment);
|
||||
}
|
||||
|
||||
static class TestCustomizer implements StructureLoggingJsonMembersCustomizer<String> {
|
||||
|
||||
@Override
|
||||
public void customize(Members<String> members) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue