Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let 'logging.structured.json.customizer' accept multiple Customizers #43368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.logging.structured;

import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -32,9 +33,10 @@
* @param customizer the fully qualified name of a
* {@link StructuredLoggingJsonMembersCustomizer}
* @author Phillip Webb
* @author Yanming Zhou
*/
record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude, Map<String, String> rename,
Map<String, String> add, Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer) {
Map<String, String> add, List<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizer) {

static StructuredLoggingJsonProperties get(Environment environment) {
return Binder.get(environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.logging.structured;

import java.util.List;
import java.util.Map;

import org.springframework.boot.json.JsonWriter.MemberPath;
Expand All @@ -28,6 +29,7 @@
* {@link StructuredLoggingJsonProperties}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
class StructuredLoggingJsonPropertiesJsonMembersCustomizer implements StructuredLoggingJsonMembersCustomizer<Object> {

Expand All @@ -49,9 +51,9 @@ public void customize(Members<Object> members) {
if (!CollectionUtils.isEmpty(add)) {
add.forEach(members::add);
}
Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer = this.properties.customizer();
List<Class<? extends StructuredLoggingJsonMembersCustomizer<?>>> customizer = this.properties.customizer();
if (customizer != null) {
createAndApplyCustomizer(members, customizer);
customizer.forEach((c) -> createAndApplyCustomizer(members, c));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.logging.structured;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -37,6 +38,7 @@
* Tests for {@link StructuredLoggingJsonPropertiesJsonMembersCustomizer}.
*
* @author Phillip Webb
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class StructuredLoggingJsonPropertiesJsonMembersCustomizerTests {
Expand Down Expand Up @@ -102,12 +104,25 @@ void customizeWhenHasCustomizerCustomizesMember() {
.applyingNameProcessor(NameProcessor.of(String::toUpperCase));
given(((Instantiator) this.instantiator).instantiateType(TestCustomizer.class)).willReturn(uppercaseCustomizer);
StructuredLoggingJsonProperties properties = new StructuredLoggingJsonProperties(Collections.emptySet(),
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), TestCustomizer.class);
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(), List.of(TestCustomizer.class));
StructuredLoggingJsonPropertiesJsonMembersCustomizer customizer = new StructuredLoggingJsonPropertiesJsonMembersCustomizer(
this.instantiator, properties);
assertThat(writeSampleJson(customizer)).contains("\"A\":\"a\"");
}

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
void multipleCustomizers() {
given(((Instantiator) this.instantiator).instantiateType(FooCustomizer.class)).willReturn(new FooCustomizer());
given(((Instantiator) this.instantiator).instantiateType(BarCustomizer.class)).willReturn(new BarCustomizer());
StructuredLoggingJsonProperties properties = new StructuredLoggingJsonProperties(Collections.emptySet(),
Collections.emptySet(), Collections.emptyMap(), Collections.emptyMap(),
List.of(FooCustomizer.class, BarCustomizer.class));
StructuredLoggingJsonPropertiesJsonMembersCustomizer customizer = new StructuredLoggingJsonPropertiesJsonMembersCustomizer(
this.instantiator, properties);
assertThat(writeSampleJson(customizer)).contains("\"foo\":\"foo\"").contains("\"bar\":\"bar\"");
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private String writeSampleJson(StructuredLoggingJsonMembersCustomizer customizer) {
return JsonWriter.of((members) -> {
Expand All @@ -126,4 +141,22 @@ public void customize(Members<String> members) {

}

static class FooCustomizer implements StructuredLoggingJsonMembersCustomizer<String> {

@Override
public void customize(Members<String> members) {
members.add("foo", "foo");
}

}

static class BarCustomizer implements StructuredLoggingJsonMembersCustomizer<String> {

@Override
public void customize(Members<String> members) {
members.add("bar", "bar");
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.logging.structured;

import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -43,7 +44,7 @@ void getBindsFromEnvironment() {
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"), TestCustomizer.class));
Map.of("e", "f"), Map.of("g", "h"), List.of(TestCustomizer.class)));
}

@Test
Expand Down
Loading