@Override
    public String formatRequest(RequestOnlyLog log) {
        requireNonNull(log, "log");

        final int flags = log.availabilityStamp();
        final RequestContext ctx = log.context();
        if (!RequestLogProperty.REQUEST_START_TIME.isAvailable(flags)) {
            return "{\"type\": \"request\"}";
        }

        try {
            String requestCauseString = null;
            if (RequestLogProperty.REQUEST_CAUSE.isAvailable(flags)) {
                final Throwable cause = log.requestCause();
                if (cause != null) {
                    requestCauseString = cause.toString();
                }
            }

            final JsonNode sanitizedHeaders;
            if (RequestLogProperty.REQUEST_HEADERS.isAvailable(flags)) {
                sanitizedHeaders = requestHeadersSanitizer.apply(ctx, log.requestHeaders());
            } else {
                sanitizedHeaders = null;
            }

            JsonNode sanitizedPreview = null;
            if (RequestLogProperty.REQUEST_CONTENT_PREVIEW.isAvailable(flags)) {
                final String contentPreview = log.requestContentPreview();
                if (contentPreview != null) {
                    sanitizedPreview = requestContentSanitizer.apply(ctx, contentPreview);
                }
            }
            JsonNode sanitizedContent = null;
            if (RequestLogProperty.REQUEST_CONTENT.isAvailable(flags)) {
                final Object content = log.requestContent();
                if (content != null) {
                    sanitizedContent = requestContentSanitizer.apply(ctx, content);
                }
            }

            final JsonNode sanitizedTrailers;
            if (RequestLogProperty.REQUEST_TRAILERS.isAvailable(flags) &&
                !log.requestTrailers().isEmpty()) {
                sanitizedTrailers = requestTrailersSanitizer.apply(ctx, log.requestTrailers());
            } else {
                sanitizedTrailers = null;
            }

            final ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("type", "request");
            objectNode.put("startTime",
                           TextFormatter.epochMicros(log.requestStartTimeMicros()).toString());

            if (RequestLogProperty.SESSION.isAvailable(flags)) {
                final ObjectNode connectionNode =
                        maybeCreateConnectionTimings(log.connectionTimings(), objectMapper);
                if (connectionNode != null) {
                    objectNode.set("connection", connectionNode);
                }
            }

            if (RequestLogProperty.REQUEST_LENGTH.isAvailable(flags)) {
                objectNode.put("length", TextFormatter.size(log.requestLength()).toString());
            }

            if (RequestLogProperty.REQUEST_END_TIME.isAvailable(flags)) {
                objectNode.put("duration",
                               TextFormatter.elapsed(log.requestDurationNanos()).toString());
            }

            if (requestCauseString != null) {
                objectNode.put("cause", requestCauseString);
            }

            if (RequestLogProperty.SCHEME.isAvailable(flags)) {
                objectNode.put("scheme", log.scheme().uriText());
            } else if (RequestLogProperty.SESSION.isAvailable(flags)) {
                objectNode.put("scheme",
                               SerializationFormat.UNKNOWN.uriText() + '+' +
                               log.sessionProtocol());
            } else {
                objectNode.put("scheme",
                               SerializationFormat.UNKNOWN.uriText() + "+unknown");
            }

            if (RequestLogProperty.NAME.isAvailable(flags)) {
                objectNode.put("name", log.name());
            }

            if (sanitizedHeaders != null) {
                objectNode.set("headers", sanitizedHeaders);
            }

            if (sanitizedPreview != null) {
                objectNode.set("preview", sanitizedPreview);
            }

            if (sanitizedContent != null) {
                objectNode.set("content", sanitizedContent);
            }

            if (sanitizedTrailers != null) {
                objectNode.set("trailers", sanitizedTrailers);
            }

            final int currentAttempt = log.currentAttempt();
            if (currentAttempt > 0) {
                objectNode.put("currentAttempt", currentAttempt);
            }

            return objectMapper.writeValueAsString(objectNode);
        } catch (Exception e) {
            logger.warn("Unexpected exception while formatting a request log: {}", log, e);
            return "{}";
        }
    }
A formatter that converts a {@link RequestOnlyLog} or {@link RequestLog} into a JSON format message.
core/src/main/java/com/linecorp/armeria/common/logging/JsonLogFormatter.java