Skip to content

Commit

Permalink
log.body.parameters as stringArray
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-suhorukov committed Jul 25, 2024
1 parent ee2d76f commit e35b9f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.slf4j.Marker;
import org.slf4j.event.KeyValuePair;

Expand Down Expand Up @@ -202,10 +204,9 @@ void captureMdcAttributes(AttributesBuilder attributes, Map<String, String> mdcP
void captureArguments(AttributesBuilder attributes, String message, Object[] arguments) {
String bodyKey = "log.body.template";
attributes.put(bodyKey, message);
for (int idx = 0; idx < arguments.length; idx++) {
Object argument = arguments[idx];
propagateAttribute(attributes, String.format("log.body.parameters.%d", idx), argument);
}
attributes.put(
AttributeKey.stringArrayKey("log.body.parameters"),
Arrays.stream(arguments).map(String::valueOf).collect(Collectors.toList()));
}

public static AttributeKey<String> getMdcAttributeKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,20 @@ void arguments() {
assertThat(logData.getBody().asString())
.isEqualTo(
"log message 'world' and 3.141592653589793, bool true, long 9223372036854775807");
assertThat(logData.getAttributes().size()).isEqualTo(9);
assertThat(logData.getAttributes().size()).isEqualTo(6);
assertThat(logData.getAttributes())
.hasEntrySatisfying(
AttributeKey.stringArrayKey("log.body.parameters"),
value ->
assertThat(value)
.isEqualTo(
Arrays.asList(
"'world'",
String.valueOf(Math.PI),
String.valueOf(true),
String.valueOf(Long.MAX_VALUE))));
assertThat(logData)
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("log.body.parameters.0"), "'world'"),
equalTo(AttributeKey.doubleKey("log.body.parameters.1"), Math.PI),
equalTo(AttributeKey.booleanKey("log.body.parameters.2"), true),
equalTo(AttributeKey.longKey("log.body.parameters.3"), Long.MAX_VALUE),
equalTo(
AttributeKey.stringKey("log.body.template"),
"log message {} and {}, bool {}, long {}"));
Expand Down

0 comments on commit e35b9f0

Please sign in to comment.