|
|
@ -18,7 +18,6 @@ package org.springframework.boot.logging;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UncheckedIOException;
|
|
|
|
import java.io.UncheckedIOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Iterator;
|
|
|
@ -36,7 +35,7 @@ import org.springframework.util.StringUtils;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Utility class that can be used to format a correlation identifier for logging based on
|
|
|
|
* Utility class that can be used to format a correlation identifier for logging based on
|
|
|
|
* <a href=
|
|
|
|
* <a href=
|
|
|
|
* "https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers">w3c</a>
|
|
|
|
* "https://www.w3.org/TR/trace-context/#examples-of-http-traceparent-headers">W3C</a>
|
|
|
|
* recommendations.
|
|
|
|
* recommendations.
|
|
|
|
* <p>
|
|
|
|
* <p>
|
|
|
|
* The formatter can be configured with a comma-separated list of names and the expected
|
|
|
|
* The formatter can be configured with a comma-separated list of names and the expected
|
|
|
@ -46,8 +45,8 @@ import org.springframework.util.StringUtils;
|
|
|
|
* {@code 16} respectively.
|
|
|
|
* {@code 16} respectively.
|
|
|
|
* <p>
|
|
|
|
* <p>
|
|
|
|
* Correlation IDs are formatted as dash separated strings surrounded in square brackets.
|
|
|
|
* Correlation IDs are formatted as dash separated strings surrounded in square brackets.
|
|
|
|
* Formatted output is always of a fixed width and with trailing whitespace. Dashes are
|
|
|
|
* Formatted output is always of a fixed width and with trailing space. Dashes are omitted
|
|
|
|
* omitted if none of the named items can be resolved.
|
|
|
|
* if none of the named items can be resolved.
|
|
|
|
* <p>
|
|
|
|
* <p>
|
|
|
|
* The following example would return a formatted result of
|
|
|
|
* The following example would return a formatted result of
|
|
|
|
* {@code "[01234567890123456789012345678901-0123456789012345] "}: <pre class="code">
|
|
|
|
* {@code "[01234567890123456789012345678901-0123456789012345] "}: <pre class="code">
|
|
|
@ -101,10 +100,12 @@ public final class CorrelationIdFormatter {
|
|
|
|
Predicate<Part> canResolve = (part) -> StringUtils.hasLength(resolver.apply(part.name()));
|
|
|
|
Predicate<Part> canResolve = (part) -> StringUtils.hasLength(resolver.apply(part.name()));
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (this.parts.stream().anyMatch(canResolve)) {
|
|
|
|
if (this.parts.stream().anyMatch(canResolve)) {
|
|
|
|
appendable.append("[");
|
|
|
|
appendable.append('[');
|
|
|
|
for (Iterator<Part> iterator = this.parts.iterator(); iterator.hasNext();) {
|
|
|
|
for (Iterator<Part> iterator = this.parts.iterator(); iterator.hasNext();) {
|
|
|
|
appendable.append(iterator.next().resolve(resolver));
|
|
|
|
appendable.append(iterator.next().resolve(resolver));
|
|
|
|
appendable.append((!iterator.hasNext()) ? "" : "-");
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
|
|
|
|
appendable.append('-');
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendable.append("] ");
|
|
|
|
appendable.append("] ");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -124,7 +125,7 @@ public final class CorrelationIdFormatter {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Create a new {@link CorrelationIdFormatter} instance from the given specification.
|
|
|
|
* Create a new {@link CorrelationIdFormatter} instance from the given specification.
|
|
|
|
* @param spec a comma separated specification
|
|
|
|
* @param spec a comma-separated specification
|
|
|
|
* @return a new {@link CorrelationIdFormatter} instance
|
|
|
|
* @return a new {@link CorrelationIdFormatter} instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static CorrelationIdFormatter of(String spec) {
|
|
|
|
public static CorrelationIdFormatter of(String spec) {
|
|
|
@ -142,7 +143,7 @@ public final class CorrelationIdFormatter {
|
|
|
|
* @return a new {@link CorrelationIdFormatter} instance
|
|
|
|
* @return a new {@link CorrelationIdFormatter} instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static CorrelationIdFormatter of(String[] spec) {
|
|
|
|
public static CorrelationIdFormatter of(String[] spec) {
|
|
|
|
return of((spec != null) ? Arrays.asList(spec) : Collections.emptyList());
|
|
|
|
return of((spec != null) ? List.of(spec) : Collections.emptyList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -166,7 +167,7 @@ public final class CorrelationIdFormatter {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
record Part(String name, int length) {
|
|
|
|
record Part(String name, int length) {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Pattern pattern = Pattern.compile("^(.+?)\\((\\d+)\\)?$");
|
|
|
|
private static final Pattern pattern = Pattern.compile("^(.+?)\\((\\d+)\\)$");
|
|
|
|
|
|
|
|
|
|
|
|
String resolve(Function<String, String> resolver) {
|
|
|
|
String resolve(Function<String, String> resolver) {
|
|
|
|
String resolved = resolver.apply(name());
|
|
|
|
String resolved = resolver.apply(name());
|
|
|
@ -174,7 +175,7 @@ public final class CorrelationIdFormatter {
|
|
|
|
return blank();
|
|
|
|
return blank();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int padding = length() - resolved.length();
|
|
|
|
int padding = length() - resolved.length();
|
|
|
|
return resolved + " ".repeat((padding > 0) ? padding : 0);
|
|
|
|
return (padding <= 0) ? resolved : resolved + " ".repeat(padding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String blank() {
|
|
|
|
String blank() {
|
|
|
|