Skip to content

Commit

Permalink
Merge pull request #43203 from izeye
Browse files Browse the repository at this point in the history
* pr/43203:
  Replace "structure logging" with "structured logging"

Closes gh-43203
  • Loading branch information
philwebb committed Nov 16, 2024
2 parents 68022ef + 0b854bf commit 3792772
Show file tree
Hide file tree
Showing 32 changed files with 66 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ logging:
corpname: mycorp
----

TIP: For more advanced customizations, you can write your own class that implements the javadoc:org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer[] interface and declare it using the configprop:logging.structured.json.customizer[] property.
TIP: For more advanced customizations, you can write your own class that implements the javadoc:org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer[] interface and declare it using the configprop:logging.structured.json.customizer[] property.
You can also declare implementations by listing them in a `META-INF/spring.factories` file.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.core.env.Environment;
import org.springframework.util.ObjectUtils;

Expand All @@ -43,7 +43,7 @@
class ElasticCommonSchemaStructuredLogFormatter extends JsonWriterStructuredLogFormatter<LogEvent> {

ElasticCommonSchemaStructuredLogFormatter(Environment environment,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
super((members) -> jsonMembers(environment, members), customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.core.env.Environment;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -70,7 +70,7 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
private static final Set<String> ADDITIONAL_FIELD_ILLEGAL_KEYS = Set.of("id", "_id");

GraylogExtendedLogFormatStructuredLogFormatter(Environment environment,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
super((members) -> jsonMembers(environment, members), customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.springframework.boot.json.JsonWriter;
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.util.CollectionUtils;

/**
Expand All @@ -44,7 +44,7 @@
*/
class LogstashStructuredLogFormatter extends JsonWriterStructuredLogFormatter<LogEvent> {

LogstashStructuredLogFormatter(StructureLoggingJsonMembersCustomizer<?> customizer) {
LogstashStructuredLogFormatter(StructuredLoggingJsonMembersCustomizer<?> customizer) {
super(LogstashStructuredLogFormatter::jsonMembers, customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import org.apache.logging.log4j.core.layout.AbstractStringLayout;

import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLogFormatterFactory;
import org.springframework.boot.logging.structured.StructuredLogFormatterFactory.CommonFormatters;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.boot.util.Instantiator;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -111,21 +111,21 @@ private void addCommonFormatters(CommonFormatters<LogEvent> commonFormatters) {

private ElasticCommonSchemaStructuredLogFormatter createEcsFormatter(Instantiator<?> instantiator) {
Environment environment = instantiator.getArg(Environment.class);
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new ElasticCommonSchemaStructuredLogFormatter(environment, jsonMembersCustomizer);
}

private GraylogExtendedLogFormatStructuredLogFormatter createGraylogFormatter(Instantiator<?> instantiator) {
Environment environment = instantiator.getArg(Environment.class);
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new GraylogExtendedLogFormatStructuredLogFormatter(environment, jsonMembersCustomizer);
}

private LogstashStructuredLogFormatter createLogstashFormatter(Instantiator<?> instantiator) {
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new LogstashStructuredLogFormatter(jsonMembersCustomizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.ElasticCommonSchemaProperties;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.core.env.Environment;

/**
Expand All @@ -45,7 +45,7 @@ class ElasticCommonSchemaStructuredLogFormatter extends JsonWriterStructuredLogF
(pair) -> pair.value);

ElasticCommonSchemaStructuredLogFormatter(Environment environment, ThrowableProxyConverter throwableProxyConverter,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
super((members) -> jsonMembers(environment, throwableProxyConverter, members), customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.GraylogExtendedLogFormatProperties;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.core.env.Environment;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -70,7 +70,7 @@ class GraylogExtendedLogFormatStructuredLogFormatter extends JsonWriterStructure
private static final Set<String> ADDITIONAL_FIELD_ILLEGAL_KEYS = Set.of("id", "_id");

GraylogExtendedLogFormatStructuredLogFormatter(Environment environment,
ThrowableProxyConverter throwableProxyConverter, StructureLoggingJsonMembersCustomizer<?> customizer) {
ThrowableProxyConverter throwableProxyConverter, StructuredLoggingJsonMembersCustomizer<?> customizer) {
super((members) -> jsonMembers(environment, throwableProxyConverter, members), customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import org.springframework.boot.json.JsonWriter.PairExtractor;
import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.JsonWriterStructuredLogFormatter;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;

/**
* Logback {@link StructuredLogFormatter} for {@link CommonStructuredLogFormat#LOGSTASH}.
Expand All @@ -50,7 +50,7 @@ class LogstashStructuredLogFormatter extends JsonWriterStructuredLogFormatter<IL
(pair) -> pair.value);

LogstashStructuredLogFormatter(ThrowableProxyConverter throwableProxyConverter,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
super((members) -> jsonMembers(throwableProxyConverter, members), customizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import ch.qos.logback.core.encoder.EncoderBase;

import org.springframework.boot.logging.structured.CommonStructuredLogFormat;
import org.springframework.boot.logging.structured.StructureLoggingJsonMembersCustomizer;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.logging.structured.StructuredLogFormatterFactory;
import org.springframework.boot.logging.structured.StructuredLogFormatterFactory.CommonFormatters;
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
import org.springframework.boot.util.Instantiator;
import org.springframework.boot.util.Instantiator.AvailableParameters;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -89,25 +89,25 @@ private void addCommonFormatters(CommonFormatters<ILoggingEvent> commonFormatter
private StructuredLogFormatter<ILoggingEvent> createEcsFormatter(Instantiator<?> instantiator) {
Environment environment = instantiator.getArg(Environment.class);
ThrowableProxyConverter throwableProxyConverter = instantiator.getArg(ThrowableProxyConverter.class);
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new ElasticCommonSchemaStructuredLogFormatter(environment, throwableProxyConverter,
jsonMembersCustomizer);
}

private StructuredLogFormatter<ILoggingEvent> createGraylogFormatter(Instantiator<?> instantiator) {
Environment environment = instantiator.getArg(Environment.class);
ThrowableProxyConverter throwableProxyConverter = instantiator.getArg(ThrowableProxyConverter.class);
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new GraylogExtendedLogFormatStructuredLogFormatter(environment, throwableProxyConverter,
jsonMembersCustomizer);
}

private StructuredLogFormatter<ILoggingEvent> createLogstashFormatter(Instantiator<?> instantiator) {
ThrowableProxyConverter throwableProxyConverter = instantiator.getArg(ThrowableProxyConverter.class);
StructureLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructureLoggingJsonMembersCustomizer.class);
StructuredLoggingJsonMembersCustomizer<?> jsonMembersCustomizer = instantiator
.getArg(StructuredLoggingJsonMembersCustomizer.class);
return new LogstashStructuredLogFormatter(throwableProxyConverter, jsonMembersCustomizer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ public abstract class JsonWriterStructuredLogFormatter<E> implements StructuredL
* @param customizer an optional customizer to apply
*/
protected JsonWriterStructuredLogFormatter(Consumer<Members<E>> members,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
this(JsonWriter.of(customized(members, customizer)).withNewLineAtEnd());
}

private static <E> Consumer<Members<E>> customized(Consumer<Members<E>> members,
StructureLoggingJsonMembersCustomizer<?> customizer) {
StructuredLoggingJsonMembersCustomizer<?> customizer) {
return (customizer != null) ? members.andThen(customizeWith(customizer)) : members;
}

@SuppressWarnings("unchecked")
private static <E> Consumer<Members<E>> customizeWith(StructureLoggingJsonMembersCustomizer<?> customizer) {
return (members) -> LambdaSafe.callback(StructureLoggingJsonMembersCustomizer.class, customizer, members)
private static <E> Consumer<Members<E>> customizeWith(StructuredLoggingJsonMembersCustomizer<?> customizer) {
return (members) -> LambdaSafe.callback(StructuredLoggingJsonMembersCustomizer.class, customizer, members)
.invoke((instance) -> instance.customize(members));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* Implementing classes can declare the following parameter types in the constructor:
* <ul>
* <li>{@link Environment}</li>
* <li>{@link StructureLoggingJsonMembersCustomizer}</li>
* <li>{@link StructuredLoggingJsonMembersCustomizer}</li>
* </ul>
* When using Logback, implementing classes can also use the following parameter types in
* the constructor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public StructuredLogFormatterFactory(Class<E> logEventType, Environment environm
this.logEventType = logEventType;
this.instantiator = new Instantiator<>(Object.class, (allAvailableParameters) -> {
allAvailableParameters.add(Environment.class, environment);
allAvailableParameters.add(StructureLoggingJsonMembersCustomizer.class,
(type) -> getStructureLoggingJsonMembersCustomizer(environment));
allAvailableParameters.add(StructuredLoggingJsonMembersCustomizer.class,
(type) -> getStructuredLoggingJsonMembersCustomizer(environment));
if (availableParameters != null) {
availableParameters.accept(allAvailableParameters);
}
Expand All @@ -92,26 +92,26 @@ public StructuredLogFormatterFactory(Class<E> logEventType, Environment environm
commonFormatters.accept(this.commonFormatters);
}

StructureLoggingJsonMembersCustomizer<?> getStructureLoggingJsonMembersCustomizer(Environment environment) {
List<StructureLoggingJsonMembersCustomizer<?>> customizers = new ArrayList<>();
StructuredLoggingJsonMembersCustomizer<?> getStructuredLoggingJsonMembersCustomizer(Environment environment) {
List<StructuredLoggingJsonMembersCustomizer<?>> customizers = new ArrayList<>();
StructuredLoggingJsonProperties properties = StructuredLoggingJsonProperties.get(environment);
if (properties != null) {
customizers.add(new StructuredLoggingJsonPropertiesJsonMembersCustomizer(this.instantiator, properties));
}
customizers.addAll(loadStructureLoggingJsonMembersCustomizers());
customizers.addAll(loadStructuredLoggingJsonMembersCustomizers());
return (members) -> invokeCustomizers(customizers, members);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private List<StructureLoggingJsonMembersCustomizer<?>> loadStructureLoggingJsonMembersCustomizers() {
return (List) this.factoriesLoader.load(StructureLoggingJsonMembersCustomizer.class,
private List<StructuredLoggingJsonMembersCustomizer<?>> loadStructuredLoggingJsonMembersCustomizers() {
return (List) this.factoriesLoader.load(StructuredLoggingJsonMembersCustomizer.class,
ArgumentResolver.from(this.instantiator::getArg));
}

@SuppressWarnings("unchecked")
private void invokeCustomizers(List<StructureLoggingJsonMembersCustomizer<?>> customizers,
private void invokeCustomizers(List<StructuredLoggingJsonMembersCustomizer<?>> customizers,
Members<Object> members) {
LambdaSafe.callbacks(StructureLoggingJsonMembersCustomizer.class, customizers, members)
LambdaSafe.callbacks(StructuredLoggingJsonMembersCustomizer.class, customizers, members)
.invoke((customizer) -> customizer.customize(members));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @see JsonWriterStructuredLogFormatter
*/
@FunctionalInterface
public interface StructureLoggingJsonMembersCustomizer<T> {
public interface StructuredLoggingJsonMembersCustomizer<T> {

/**
* Customize the given {@link Members} instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
* @param include the paths that should be included. An empty set includes all names
* @param exclude the paths that should be excluded. An empty set excludes nothing
* @param rename a map of path to replacement names
* @param add a map of additional elements {@link StructureLoggingJsonMembersCustomizer}
* @param add a map of additional elements {@link StructuredLoggingJsonMembersCustomizer}
* @param customizer the fully qualified name of a
* {@link StructureLoggingJsonMembersCustomizer}
* {@link StructuredLoggingJsonMembersCustomizer}
* @author Phillip Webb
*/
record StructuredLoggingJsonProperties(Set<String> include, Set<String> exclude, Map<String, String> rename,
Map<String, String> add, Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer) {
Map<String, String> add, 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 @@ -24,12 +24,12 @@
import org.springframework.util.CollectionUtils;

/**
* {@link StructureLoggingJsonMembersCustomizer} to apply
* {@link StructuredLoggingJsonMembersCustomizer} to apply
* {@link StructuredLoggingJsonProperties}.
*
* @author Phillip Webb
*/
class StructuredLoggingJsonPropertiesJsonMembersCustomizer implements StructureLoggingJsonMembersCustomizer<Object> {
class StructuredLoggingJsonPropertiesJsonMembersCustomizer implements StructuredLoggingJsonMembersCustomizer<Object> {

private final Instantiator<?> instantiator;

Expand All @@ -49,7 +49,7 @@ public void customize(Members<Object> members) {
if (!CollectionUtils.isEmpty(add)) {
add.forEach(members::add);
}
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizer = this.properties.customizer();
Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizer = this.properties.customizer();
if (customizer != null) {
createAndApplyCustomizer(members, customizer);
}
Expand All @@ -71,8 +71,9 @@ boolean filterPath(MemberPath path) {

@SuppressWarnings({ "unchecked", "rawtypes" })
private void createAndApplyCustomizer(Members<Object> members,
Class<? extends StructureLoggingJsonMembersCustomizer<?>> customizerClass) {
((StructureLoggingJsonMembersCustomizer) this.instantiator.instantiateType(customizerClass)).customize(members);
Class<? extends StructuredLoggingJsonMembersCustomizer<?>> customizerClass) {
((StructuredLoggingJsonMembersCustomizer) this.instantiator.instantiateType(customizerClass))
.customize(members);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
{
"name": "logging.structured.json.customizer",
"type": "java.lang.String",
"description": "The fully qualified class name of a StructureLoggingJsonMembersCustomizer"
"description": "The fully qualified class name of a StructuredLoggingJsonMembersCustomizer"
},
{
"name": "logging.structured.json.exclude",
Expand Down
Loading

0 comments on commit 3792772

Please sign in to comment.