Refine structured logging

Refine structured logging to support `Environment`, `ApplicationPid` and
`ElasticCommonSchemaService` injection. With these updates we are able
to remove the `ApplicationMetadata` class and simplify the parameters
passed to the layout/encoder classes.

Closes gh-41491
This commit is contained in:
Phillip Webb
2024-07-24 17:59:45 +01:00
parent 50dbaec2d0
commit de3b14f2b4
30 changed files with 539 additions and 290 deletions

View File

@@ -19,23 +19,23 @@ package smoketest.structuredlogging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.ThrowableProxy;
import org.springframework.boot.logging.structured.ApplicationMetadata;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.system.ApplicationPid;
public class CustomStructuredLogFormatter implements StructuredLogFormatter<LogEvent> {
private final ApplicationMetadata metadata;
private final ApplicationPid pid;
public CustomStructuredLogFormatter(ApplicationMetadata metadata) {
this.metadata = metadata;
public CustomStructuredLogFormatter(ApplicationPid pid) {
this.pid = pid;
}
@Override
public String format(LogEvent event) {
StringBuilder result = new StringBuilder();
result.append("epoch=").append(event.getInstant().getEpochMillisecond());
if (this.metadata.pid() != null) {
result.append(" pid=").append(this.metadata.pid());
if (this.pid.isAvailable()) {
result.append(" pid=").append(this.pid);
}
result.append(" msg=\"").append(event.getMessage().getFormattedMessage()).append('"');
ThrowableProxy throwable = event.getThrownProxy();

View File

@@ -20,17 +20,17 @@ import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import org.springframework.boot.logging.structured.ApplicationMetadata;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
import org.springframework.boot.system.ApplicationPid;
public class CustomStructuredLogFormatter implements StructuredLogFormatter<ILoggingEvent> {
private final ApplicationPid pid;
private final ThrowableProxyConverter throwableProxyConverter;
private final ApplicationMetadata metadata;
public CustomStructuredLogFormatter(ApplicationMetadata metadata, ThrowableProxyConverter throwableProxyConverter) {
this.metadata = metadata;
public CustomStructuredLogFormatter(ApplicationPid pid, ThrowableProxyConverter throwableProxyConverter) {
this.pid = pid;
this.throwableProxyConverter = throwableProxyConverter;
}
@@ -38,8 +38,8 @@ public class CustomStructuredLogFormatter implements StructuredLogFormatter<ILog
public String format(ILoggingEvent event) {
StringBuilder result = new StringBuilder();
result.append("epoch=").append(event.getInstant().toEpochMilli());
if (this.metadata.pid() != null) {
result.append(" pid=").append(this.metadata.pid());
if (this.pid.isAvailable()) {
result.append(" pid=").append(this.pid);
}
result.append(" msg=\"").append(event.getFormattedMessage()).append('"');
IThrowableProxy throwable = event.getThrowableProxy();