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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access Logs contents enhancement #434 #435

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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