Skip to content

Commit

Permalink
enhancement reactor#434 Access Logs contents enhancement
Browse files Browse the repository at this point in the history
Add request port to the log message.
Add duration in milliseconds between request and response.
  • Loading branch information
Erwan Letessier committed Sep 17, 2018
1 parent d3a97e8 commit 7067233
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/reactor/netty/http/server/AccessLogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

accessLog = new AccessLog()
.address(((SocketChannel) ctx.channel()).remoteAddress().getHostString())
.port(((SocketChannel) ctx.channel()).localAddress().getPort())
.method(request.method().name())
.uri(request.uri())
.protocol(request.protocolVersion().text());
Expand Down Expand Up @@ -96,7 +97,7 @@ static final class AccessLog {
static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z", Locale.US);
static final String COMMON_LOG_FORMAT =
"{} - {} [{}] \"{} {} {}\" {} {}";
"{} - {} [{}] \"{} {} {}\" {} {} {} {} ms";
static final String MISSING = "-";

final String zonedDateTime;
Expand All @@ -109,6 +110,8 @@ static final class AccessLog {
int status;
long contentLength;
boolean chunked;
long startTime = System.currentTimeMillis();
int port;

AccessLog() {
this.zonedDateTime = ZonedDateTime.now().format(DATE_TIME_FORMATTER);
Expand All @@ -119,6 +122,11 @@ AccessLog address(String address) {
return this;
}

AccessLog port(int port) {
this.port = port;
return this;
}

AccessLog method(String method) {
this.method = Objects.requireNonNull(method, "method");
return this;
Expand Down Expand Up @@ -156,10 +164,14 @@ AccessLog chunked(boolean chunked) {
return this;
}

long duration() {
return System.currentTimeMillis() - startTime;
}

void log() {
if (log.isInfoEnabled()) {
log.info(COMMON_LOG_FORMAT, address, user, zonedDateTime,
method, uri, protocol, status, (contentLength > -1 ? contentLength : MISSING));
method, uri, protocol, status, (contentLength > -1 ? contentLength : MISSING), port, duration());
}
}
}
Expand Down

0 comments on commit 7067233

Please sign in to comment.