Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an "advanced" LoggerFormatter API to expose more request metadata #2368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2014 Red Hat, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.web.handler;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

/**
* Implement to format the output of the {@link LoggerHandler}
*
* @author <a href="mailto:oded@geek.co.il">Oded Arbel</a>
*/
@VertxGen
@FunctionalInterface
public interface LoggerFormatterAdvanced {

/**
* Formats and returns the log statement
*
* @param routingContext The routing context
* @param timestamp The system time in milliseconds when the request was handled by the {@link LoggerHandler}
* @param remoteClient The remote client's host address
* @param versionFormatted The HTTP version as formatted for display (normally {@code HTTP/x.x})
* @param method The request's HTTP method
* @param uri The request's URI
* @param status The response's HTTP status code
* @param contentLength The amount of bytes that were written in the response
* @param ms The number of milliseconds since first receiving the request
* @return The formatted string to log
*/
String format(RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted,
HttpMethod method, String uri, int status, long contentLength, long ms);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static LoggerHandler create(boolean immediate, LoggerFormat format) {
*
* @deprecated Superseded by {@link #customFormatter(LoggerFormatter)}
* @param formatter the formatting function
* @return the formatted log string
* @return self
* @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM}
*/
@Deprecated
Expand All @@ -80,9 +80,19 @@ static LoggerHandler create(boolean immediate, LoggerFormat format) {
* Set the custom formatter to be used by the handler.
*
* @param formatter the formatter
* @return the formatted log string
* @return self
* @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM}
*/
@Fluent
LoggerHandler customFormatter(LoggerFormatter formatter);

/**
* Set the custom formatter to be used by the handler.
*
* @param formatter the formatter
* @return self
* @throws IllegalStateException if current format is not {@link LoggerFormat#CUSTOM}
*/
@Fluent
LoggerHandler customFormatter(LoggerFormatterAdvanced formatter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.LoggerFormat;
import io.vertx.ext.web.handler.LoggerFormatter;
import io.vertx.ext.web.handler.LoggerFormatterAdvanced;
import io.vertx.ext.web.handler.LoggerHandler;
import io.vertx.ext.web.impl.Utils;

Expand Down Expand Up @@ -57,8 +58,7 @@ public class LoggerHandlerImpl implements LoggerHandler {
*/
private final LoggerFormat format;

private Function<HttpServerRequest, String> customFormatter;
private LoggerFormatter logFormatter;
private LoggerFormatterAdvanced logFormatter;

public LoggerHandlerImpl(boolean immediate, LoggerFormat format) {
this.immediate = immediate;
Expand Down Expand Up @@ -147,11 +147,8 @@ private void log(RoutingContext context, long timestamp, String remoteClient, Ht
break;
case CUSTOM:
try {
if (logFormatter != null) {
message = logFormatter.format(context, (System.currentTimeMillis() - timestamp));
} else {
message = customFormatter.apply(request);
}
message = logFormatter.format(context, timestamp, remoteClient, versionFormatted, method, uri,
status, contentLength, (System.currentTimeMillis() - timestamp));
} catch (RuntimeException e) {
// if an error happens at the user side
// log it instead
Expand Down Expand Up @@ -196,7 +193,8 @@ public LoggerHandler customFormatter(Function<HttpServerRequest, String> formatt
throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format");
}

this.customFormatter = formatter;
this.logFormatter = (RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted,
HttpMethod method, String uri, int status, long contentLength, long ms) -> formatter.apply(routingContext.request());

return this;
}
Expand All @@ -207,7 +205,19 @@ public LoggerHandler customFormatter(LoggerFormatter formatter) {
throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format");
}

this.logFormatter = (RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted,
HttpMethod method, String uri, int status, long contentLength, long ms) -> formatter.format(routingContext, ms);
return this;
}

@Override
public LoggerHandler customFormatter(LoggerFormatterAdvanced formatter) {
if (format != LoggerFormat.CUSTOM) {
throw new IllegalStateException("Setting a formatter requires the handler to be set to CUSTOM format");
}

this.logFormatter = formatter;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.vertx.ext.web.handler;

import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.WebTestBase;
import org.junit.Test;

Expand Down Expand Up @@ -82,5 +83,17 @@ private void testLogger(LoggerHandler logger) throws Exception {
testRequest(HttpMethod.GET, "/somedir", 200, "OK");
}

@Test
public void testLogger6() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
LoggerHandler logger = LoggerHandler.create(true, LoggerFormat.CUSTOM).customFormatter((
RoutingContext routingContext, long timestamp, String remoteClient, String versionFormatted,
HttpMethod method, String uri, int status, long contentLength, long ms) -> {
latch.countDown();
return "custom log message";
});
testLogger(logger);
latch.await();
}

}