use of org.fusesource.jansi.Ansi.Color.GREEN in project restheart by SoftInstigate.
the class RequestLogger method addExchangeCompleteListener.
private void addExchangeCompleteListener(HttpServerExchange exchange, Integer logLevel, final StringBuilder sb, final long start) {
exchange.addExchangeCompleteListener((final HttpServerExchange exchange1, final ExchangeCompletionListener.NextListener nextListener) -> {
if (logLevel < 1) {
return;
}
// restore MDC context
// MDC context is put in the thread context
// A thread switch in the request handling pipeline loses the MDC context.
// TracingInstrumentationHandler adds it to the
// exchange as an Attachment
var mdcCtx = ByteArrayProxyResponse.of(exchange).getMDCContext();
if (mdcCtx != null) {
MDC.setContextMap(mdcCtx);
}
// note sc is always null if this handler is chained before
// SecurityHandlerDispacher
final SecurityContext sc = exchange1.getSecurityContext();
if (logLevel == 1) {
sb.append(" =>").append(" status=");
if (exchange.getStatusCode() >= 300 && exchange.getStatusCode() != 304) {
sb.append(ansi().fg(RED).bold().a(exchange.getStatusCode()).reset().toString());
} else {
sb.append(ansi().fg(GREEN).bold().a(exchange.getStatusCode()).reset().toString());
}
sb.append(" elapsed=").append(System.currentTimeMillis() - start).append("ms").append(" contentLength=").append(exchange1.getResponseContentLength());
if (sc != null && sc.getAuthenticatedAccount() != null) {
sb.append(" ").append(sc.getAuthenticatedAccount().toString());
}
} else if (logLevel >= 2) {
sb.append("--------------------------RESPONSE--------------------------\n");
if (sc != null) {
if (sc.isAuthenticated()) {
sb.append(" authType=").append(sc.getMechanismName()).append("\n");
sb.append(" account=").append(sc.getAuthenticatedAccount().toString()).append("\n");
} else {
sb.append(" authType=none" + "\n");
}
}
sb.append(" contentLength=").append(exchange1.getResponseContentLength()).append("\n");
sb.append(" contentType=").append(exchange1.getResponseHeaders().getFirst(Headers.CONTENT_TYPE)).append("\n");
@SuppressWarnings("deprecation") Map<String, Cookie> cookies1 = exchange1.getResponseCookies();
if (cookies1 != null) {
cookies1.values().stream().forEach((cookie) -> {
sb.append(" cookie=").append(cookie.getName()).append("=").append(cookie.getValue()).append("; domain=").append(cookie.getDomain()).append("; path=").append(cookie.getPath()).append("\n");
});
}
for (HeaderValues header : exchange1.getResponseHeaders()) {
header.stream().forEach((value) -> {
if (header.getHeaderName() != null && AUTH_TOKEN_HEADER.toString().equalsIgnoreCase(header.getHeaderName().toString())) {
value = "**********";
}
sb.append(" header=").append(header.getHeaderName()).append("=").append(value).append("\n");
});
}
sb.append(" status=");
if (exchange.getStatusCode() >= 300) {
sb.append(ansi().fg(RED).bold().a(exchange1.getStatusCode()).reset().toString());
} else {
sb.append(ansi().fg(GREEN).bold().a(exchange1.getStatusCode()).reset().toString());
}
sb.append("\n");
sb.append(" elapsed=").append(System.currentTimeMillis() - start).append("ms\n");
sb.append("==============================================================");
}
nextListener.proceed();
LOGGER.info(sb.toString());
});
}
Aggregations