Search in sources :

Example 1 with GREEN

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());
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) AUTH_TOKEN_HEADER(org.restheart.plugins.security.TokenManager.AUTH_TOKEN_HEADER) Logger(org.slf4j.Logger) URLDecoder(java.net.URLDecoder) Iterator(java.util.Iterator) RED(org.fusesource.jansi.Ansi.Color.RED) HttpServerExchange(io.undertow.server.HttpServerExchange) LoggerFactory(org.slf4j.LoggerFactory) Configuration(org.restheart.Configuration) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) Deque(java.util.Deque) Cookie(io.undertow.server.handlers.Cookie) HeaderValues(io.undertow.util.HeaderValues) QueryParameterUtils(io.undertow.util.QueryParameterUtils) LocaleUtils(io.undertow.util.LocaleUtils) JsonProxyRequest(org.restheart.exchange.JsonProxyRequest) Bootstrapper(org.restheart.Bootstrapper) GREEN(org.fusesource.jansi.Ansi.Color.GREEN) ByteArrayProxyResponse(org.restheart.exchange.ByteArrayProxyResponse) MDC(org.slf4j.MDC) Map(java.util.Map) SecurityContext(io.undertow.security.api.SecurityContext) Headers(io.undertow.util.Headers) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Ansi.ansi(org.fusesource.jansi.Ansi.ansi) SecurityContext(io.undertow.security.api.SecurityContext) HeaderValues(io.undertow.util.HeaderValues) Map(java.util.Map)

Aggregations

SecurityContext (io.undertow.security.api.SecurityContext)1 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Cookie (io.undertow.server.handlers.Cookie)1 HeaderValues (io.undertow.util.HeaderValues)1 Headers (io.undertow.util.Headers)1 LocaleUtils (io.undertow.util.LocaleUtils)1 QueryParameterUtils (io.undertow.util.QueryParameterUtils)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLDecoder (java.net.URLDecoder)1 Deque (java.util.Deque)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 GREEN (org.fusesource.jansi.Ansi.Color.GREEN)1 RED (org.fusesource.jansi.Ansi.Color.RED)1 Ansi.ansi (org.fusesource.jansi.Ansi.ansi)1 Bootstrapper (org.restheart.Bootstrapper)1 Configuration (org.restheart.Configuration)1 ByteArrayProxyResponse (org.restheart.exchange.ByteArrayProxyResponse)1 JsonProxyRequest (org.restheart.exchange.JsonProxyRequest)1