diff --git a/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatter.java b/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatter.java index da9fb8f208a..11c6de2a2a1 100644 --- a/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatter.java +++ b/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatter.java @@ -32,6 +32,7 @@ import com.linecorp.armeria.common.HttpHeaders; import com.linecorp.armeria.common.RequestContext; import com.linecorp.armeria.common.SerializationFormat; +import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.common.annotation.UnstableApi; import com.linecorp.armeria.common.util.TextFormatter; @@ -43,36 +44,39 @@ final class JsonLogFormatter implements LogFormatter { private static final Logger logger = LoggerFactory.getLogger(JsonLogFormatter.class); - static final JsonLogFormatter DEFAULT_INSTANCE = new JsonLogFormatterBuilder().build(); + static final LogFormatter DEFAULT_INSTANCE = new JsonLogFormatterBuilder().build(); - private final BiFunction + private final BiFunction requestHeadersSanitizer; - private final BiFunction + private final BiFunction responseHeadersSanitizer; - private final BiFunction + private final BiFunction requestTrailersSanitizer; - private final BiFunction + private final BiFunction responseTrailersSanitizer; - private final BiFunction requestContentSanitizer; + private final BiFunction + requestContentSanitizer; - private final BiFunction responseContentSanitizer; + private final BiFunction + responseContentSanitizer; private final ObjectMapper objectMapper; JsonLogFormatter( - BiFunction requestHeadersSanitizer, - BiFunction - responseHeadersSanitizer, - BiFunction - requestTrailersSanitizer, - BiFunction - responseTrailersSanitizer, - BiFunction requestContentSanitizer, - BiFunction responseContentSanitizer, + BiFunction requestHeadersSanitizer, + BiFunction responseHeadersSanitizer, + BiFunction requestTrailersSanitizer, + BiFunction responseTrailersSanitizer, + BiFunction requestContentSanitizer, + BiFunction responseContentSanitizer, ObjectMapper objectMapper) { this.requestHeadersSanitizer = requestHeadersSanitizer; this.responseHeadersSanitizer = responseHeadersSanitizer; diff --git a/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatterBuilder.java b/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatterBuilder.java index c01e14eb997..5c1a177bea9 100644 --- a/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatterBuilder.java +++ b/core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatterBuilder.java @@ -51,60 +51,61 @@ public JsonLogFormatterBuilder objectMapper(ObjectMapper objectMapper) { @Override public JsonLogFormatterBuilder requestHeadersSanitizer( - BiFunction + BiFunction requestHeadersSanitizer) { return (JsonLogFormatterBuilder) super.requestHeadersSanitizer(requestHeadersSanitizer); } @Override public JsonLogFormatterBuilder responseHeadersSanitizer( - BiFunction + BiFunction responseHeadersSanitizer) { return (JsonLogFormatterBuilder) super.responseHeadersSanitizer(responseHeadersSanitizer); } @Override public JsonLogFormatterBuilder requestTrailersSanitizer( - BiFunction + BiFunction requestTrailersSanitizer) { return (JsonLogFormatterBuilder) super.requestTrailersSanitizer(requestTrailersSanitizer); } @Override public JsonLogFormatterBuilder responseTrailersSanitizer( - BiFunction + BiFunction responseTrailersSanitizer) { return (JsonLogFormatterBuilder) super.responseTrailersSanitizer(responseTrailersSanitizer); } @Override public JsonLogFormatterBuilder headersSanitizer( - BiFunction headersSanitizer) { + BiFunction + headersSanitizer) { return (JsonLogFormatterBuilder) super.headersSanitizer(headersSanitizer); } @Override public JsonLogFormatterBuilder requestContentSanitizer( - BiFunction requestContentSanitizer) { + BiFunction requestContentSanitizer) { return (JsonLogFormatterBuilder) super.requestContentSanitizer(requestContentSanitizer); } @Override public JsonLogFormatterBuilder responseContentSanitizer( - BiFunction responseContentSanitizer) { + BiFunction responseContentSanitizer) { return (JsonLogFormatterBuilder) super.responseContentSanitizer(responseContentSanitizer); } @Override public JsonLogFormatterBuilder contentSanitizer( - BiFunction contentSanitizer) { + BiFunction contentSanitizer) { return (JsonLogFormatterBuilder) super.contentSanitizer(contentSanitizer); } /** - * Returns a newly-created {@link LogFormatter} based on the properties of this builder. + * Returns a newly-created JSON {@link LogFormatter} based on the properties of this builder. */ - public JsonLogFormatter build() { + public LogFormatter build() { final ObjectMapper objectMapper = this.objectMapper != null ? this.objectMapper : JacksonUtil.newDefaultObjectMapper(); final BiFunction defaultHeadersSanitizer = diff --git a/core/src/main/java/com/linecorp/armeria/common/logging/LoggingDecoratorBuilder.java b/core/src/main/java/com/linecorp/armeria/common/logging/LoggingDecoratorBuilder.java index 77fdc12dc04..a5f40681053 100644 --- a/core/src/main/java/com/linecorp/armeria/common/logging/LoggingDecoratorBuilder.java +++ b/core/src/main/java/com/linecorp/armeria/common/logging/LoggingDecoratorBuilder.java @@ -546,7 +546,7 @@ protected final LogWriter logWriter() { if (!buildLogWriter) { return LogWriter.of(); } - final TextLogFormatter logFormatter = + final LogFormatter logFormatter = LogFormatter.builderForText() .requestHeadersSanitizer(convertToStringSanitizer(requestHeadersSanitizer)) .responseHeadersSanitizer(convertToStringSanitizer(responseHeadersSanitizer)) diff --git a/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatter.java b/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatter.java index f7eab809b19..00939ce4336 100644 --- a/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatter.java +++ b/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatter.java @@ -26,6 +26,7 @@ import com.linecorp.armeria.common.HttpHeaders; import com.linecorp.armeria.common.RequestContext; import com.linecorp.armeria.common.SerializationFormat; +import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.common.annotation.UnstableApi; import com.linecorp.armeria.common.util.TextFormatter; import com.linecorp.armeria.internal.common.util.TemporaryThreadLocals; @@ -36,32 +37,41 @@ @UnstableApi final class TextLogFormatter implements LogFormatter { - static final TextLogFormatter DEFAULT_INSTANCE = new TextLogFormatterBuilder().build(); + static final LogFormatter DEFAULT_INSTANCE = new TextLogFormatterBuilder().build(); - private final BiFunction - requestHeadersSanitizer; + private final BiFunction requestHeadersSanitizer; - private final BiFunction - responseHeadersSanitizer; + private final BiFunction responseHeadersSanitizer; - private final BiFunction - requestTrailersSanitizer; + private final BiFunction requestTrailersSanitizer; - private final BiFunction - responseTrailersSanitizer; + private final BiFunction responseTrailersSanitizer; - private final BiFunction requestContentSanitizer; + private final BiFunction requestContentSanitizer; + + private final BiFunction responseContentSanitizer; - private final BiFunction responseContentSanitizer; private final boolean includeContext; TextLogFormatter( - BiFunction requestHeadersSanitizer, - BiFunction responseHeadersSanitizer, - BiFunction requestTrailersSanitizer, - BiFunction responseTrailersSanitizer, - BiFunction requestContentSanitizer, - BiFunction responseContentSanitizer, + BiFunction requestHeadersSanitizer, + BiFunction responseHeadersSanitizer, + BiFunction requestTrailersSanitizer, + BiFunction responseTrailersSanitizer, + BiFunction requestContentSanitizer, + BiFunction responseContentSanitizer, boolean includeContext) { this.requestHeadersSanitizer = requestHeadersSanitizer; this.responseHeadersSanitizer = responseHeadersSanitizer; @@ -282,7 +292,7 @@ public String formatResponse(RequestLog log) { buf.append('}'); final List children = log.children(); - final int numChildren = children != null ? children.size() : 0; + final int numChildren = children.size(); if (numChildren > 1) { // Append only when there were retries which the numChildren is greater than 1. buf.append(", {totalAttempts="); diff --git a/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatterBuilder.java b/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatterBuilder.java index 2a7692fed03..1ffef054772 100644 --- a/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatterBuilder.java +++ b/core/src/main/java/com/linecorp/armeria/common/logging/TextLogFormatterBuilder.java @@ -22,6 +22,7 @@ import com.linecorp.armeria.common.HttpHeaders; import com.linecorp.armeria.common.RequestContext; +import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.common.annotation.UnstableApi; /** @@ -36,52 +37,54 @@ public final class TextLogFormatterBuilder extends AbstractLogFormatterBuilder requestHeadersSanitizer) { + BiFunction requestHeadersSanitizer) { return (TextLogFormatterBuilder) super.requestHeadersSanitizer(requestHeadersSanitizer); } @Override public TextLogFormatterBuilder responseHeadersSanitizer( - BiFunction - responseHeadersSanitizer) { + BiFunction responseHeadersSanitizer) { return (TextLogFormatterBuilder) super.responseHeadersSanitizer(responseHeadersSanitizer); } @Override public TextLogFormatterBuilder requestTrailersSanitizer( - BiFunction - requestTrailersSanitizer) { + BiFunction requestTrailersSanitizer) { return (TextLogFormatterBuilder) super.requestTrailersSanitizer(requestTrailersSanitizer); } @Override public TextLogFormatterBuilder responseTrailersSanitizer( - BiFunction - responseTrailersSanitizer) { + BiFunction responseTrailersSanitizer) { return (TextLogFormatterBuilder) super.responseTrailersSanitizer(responseTrailersSanitizer); } @Override public TextLogFormatterBuilder headersSanitizer( - BiFunction headersSanitizer) { + BiFunction headersSanitizer) { return (TextLogFormatterBuilder) super.headersSanitizer(headersSanitizer); } @Override public TextLogFormatterBuilder requestContentSanitizer( - BiFunction requestContentSanitizer) { + BiFunction requestContentSanitizer) { return (TextLogFormatterBuilder) super.requestContentSanitizer(requestContentSanitizer); } @Override public TextLogFormatterBuilder responseContentSanitizer( - BiFunction responseContentSanitizer) { + BiFunction responseContentSanitizer) { return (TextLogFormatterBuilder) super.responseContentSanitizer(responseContentSanitizer); } @Override public TextLogFormatterBuilder contentSanitizer( - BiFunction contentSanitizer) { + BiFunction contentSanitizer) { return (TextLogFormatterBuilder) super.contentSanitizer(contentSanitizer); } @@ -96,9 +99,9 @@ public TextLogFormatterBuilder includeContext(boolean includeContext) { } /** - * Returns a newly-created {@link TextLogFormatter} based on the properties of this builder. + * Returns a newly-created text {@link LogFormatter} based on the properties of this builder. */ - public TextLogFormatter build() { + public LogFormatter build() { return new TextLogFormatter( firstNonNull(requestHeadersSanitizer(), defaultSanitizer()), firstNonNull(responseHeadersSanitizer(), defaultSanitizer()), diff --git a/kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/Jsr305StrictTest.kt b/kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/Jsr305StrictTest.kt index 3ee9bcbf83d..256bea1ea81 100644 --- a/kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/Jsr305StrictTest.kt +++ b/kotlin/src/test/kotlin/com/linecorp/armeria/server/kotlin/Jsr305StrictTest.kt @@ -16,6 +16,7 @@ package com.linecorp.armeria.server.kotlin +import com.linecorp.armeria.common.logging.LogFormatter import com.linecorp.armeria.server.logging.LoggingService import org.junit.jupiter.api.Test @@ -35,5 +36,25 @@ class Jsr305StrictTest { .responseContentSanitizer { _, _ -> null } .contentSanitizer { _, _ -> null } .responseCauseSanitizer { _, _ -> null } + + LogFormatter.builderForText() + .requestHeadersSanitizer { _, _ -> null } + .responseHeadersSanitizer { _, _ -> null } + .requestTrailersSanitizer { _, _ -> null } + .responseTrailersSanitizer { _, _ -> null } + .headersSanitizer { _, _ -> null } + .requestContentSanitizer { _, _ -> null } + .responseContentSanitizer { _, _ -> null } + .contentSanitizer { _, _ -> null } + + LogFormatter.builderForJson() + .requestHeadersSanitizer { _, _ -> null } + .responseHeadersSanitizer { _, _ -> null } + .requestTrailersSanitizer { _, _ -> null } + .responseTrailersSanitizer { _, _ -> null } + .headersSanitizer { _, _ -> null } + .requestContentSanitizer { _, _ -> null } + .responseContentSanitizer { _, _ -> null } + .contentSanitizer { _, _ -> null } } }