@Override
    public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
                         final FilterChain filterChain) throws IOException, ServletException {
        try {
            val request = (HttpServletRequest) servletRequest;
            val response = (HttpServletResponse) servletResponse;

            addContextAttribute("remoteAddress", request.getRemoteAddr());
            addContextAttribute("remoteUser", request.getRemoteUser());
            addContextAttribute("serverName", request.getServerName());
            addContextAttribute("serverPort", String.valueOf(request.getServerPort()));
            addContextAttribute("locale", request.getLocale().getDisplayName());
            addContextAttribute("contentType", request.getContentType());
            addContextAttribute("contextPath", request.getContextPath());
            addContextAttribute("localAddress", request.getLocalAddr());
            addContextAttribute("localPort", String.valueOf(request.getLocalPort()));
            addContextAttribute("remotePort", String.valueOf(request.getRemotePort()));
            addContextAttribute("pathInfo", request.getPathInfo());
            addContextAttribute("protocol", request.getProtocol());
            addContextAttribute("authType", request.getAuthType());
            addContextAttribute("method", request.getMethod());
            addContextAttribute("queryString", request.getQueryString());
            addContextAttribute("requestUri", request.getRequestURI());
            addContextAttribute("scheme", request.getScheme());
            addContextAttribute("timezone", TimeZone.getDefault().getDisplayName());

            val requestId = UUID.randomUUID().toString();
            addContextAttribute("requestId", requestId);
            request.setAttribute("requestId", requestId);
            response.setHeader("X-RequestId", requestId);

            Optional.ofNullable(request.getSession(false)).ifPresent(session -> {
                addContextAttribute("sessionId", session.getId());
                request.setAttribute("sessionId", session.getId());
                response.setHeader("X-SessionId", session.getId());
            });
            
            val params = request.getParameterMap();
            val mdc = casProperties.getLogging().getMdc();
            params.keySet()
                .stream()
                .filter(parameterName -> mdc.getParametersToExclude().stream().noneMatch(p -> RegexUtils.find(parameterName, p)))
                .forEach(parameterName -> {
                    val values = params.get(parameterName);
                    addContextAttribute(parameterName, Arrays.toString(values));
                });

            Collections.list(request.getAttributeNames()).forEach(a -> addContextAttribute(a, request.getAttribute(a)));
            val requestHeaderNames = request.getHeaderNames();
            FunctionUtils.doIfNotNull(requestHeaderNames,
                __ -> Collections.list(requestHeaderNames)
                    .stream()
                    .filter(header -> mdc.getHeadersToExclude().stream().noneMatch(excludedHeader -> RegexUtils.find(header, excludedHeader)))
                    .forEach(h -> addContextAttribute(h, request.getHeader(h))));

            ticketGrantingTicketCookieGenerator.ifAvailable(builder -> {
                val cookieValue = builder.retrieveCookieValue(request);
                if (StringUtils.isNotBlank(cookieValue)) {
                    val principal = ticketRegistrySupport.getObject().getAuthenticatedPrincipalFrom(cookieValue);
                    FunctionUtils.doIfNotNull(principal, __ -> addContextAttribute("principal", principal.getId()));
                }
            });
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            MDC.clear();
        }
    }
This is {@link ThreadContextMDCServletFilter}.

@author Misagh Moayyed
@since 5.0.0
core/cas-server-core-logging/src/main/java/org/apereo/cas/logging/web/ThreadContextMDCServletFilter.java