Created
March 30, 2017 12:48
-
-
Save krrrr38/4115c2e53c5fc08d7d62bc376b0068d3 to your computer and use it in GitHub Desktop.
armeria response logger with LTSV format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ResponseLTSVLoggingService<I extends Request, O extends Response> | |
extends DecoratingService<I, O, I, O> { | |
public ResponseLTSVLoggingService(Service<? super I, ? extends O> delegate) { | |
super(delegate); | |
} | |
@Override | |
public O serve(ServiceRequestContext ctx, I req) throws Exception { | |
ctx.log().addListener(this::logResponse, RequestLogAvailability.COMPLETE); | |
return delegate().serve(ctx, req); | |
} | |
protected void logResponse(RequestLog log) { | |
final Logger logger = ((ServiceRequestContext) log.context()).logger(); | |
logger.info(responseLogLTSV((DefaultRequestLog) log)); | |
} | |
private String responseLogLTSV(DefaultRequestLog log) { | |
final HttpHeaders headers = (HttpHeaders) log.requestEnvelope(); | |
final String userAgent = headers.get(HttpHeaderNames.USER_AGENT); | |
final String scheme = headers.get(HttpHeaderNames.SCHEME); | |
final String authority = headers.get(HttpHeaderNames.AUTHORITY); | |
final StringBuilder sb = new StringBuilder(); | |
// request | |
sb.append("time:"); | |
TextFormatter.appendEpoch(sb, log.requestStartTimeMillis()); | |
sb.append("\t"); | |
sb.append("method:").append(log.method()).append("\t"); | |
sb.append("scheme:").append(scheme).append("\t"); | |
sb.append("host:").append(log.host()).append("\t"); | |
sb.append("authority:").append(authority).append("\t"); | |
sb.append("path:").append(log.path()).append("\t"); | |
sb.append("request_length:").append(log.requestLength()).append("\t"); | |
sb.append("user_agent:").append(userAgent).append("\t"); | |
// response | |
sb.append("status:").append(log.statusCode()).append("\t"); | |
sb.append("duration_millis:").append(log.totalDurationNanos() / 1000_000).append("\t"); | |
sb.append("response_length:").append(log.responseLength()).append("\t"); | |
sb.append("response_cause:").append(log.responseCause()); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment