Skip to content

Commit

Permalink
Provide servlet request listener which fill the MDC
Browse files Browse the repository at this point in the history
This is seen as superior to a filter as the MDC is not cleared before an error handler servlet is executed.

Signed-off-by: Sven Strickroth <[email protected]>
  • Loading branch information
csware committed Jan 19, 2024
1 parent f3efb8f commit f1ba14a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, 2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.helpers;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.MDC;

import ch.qos.logback.classic.ClassicConstants;

/**
* A simple servlet request listener that stores the remote address,
* used HTTP method, and
*
* <p> The value is removed from the MDC once the request has
* been fully processed (including an error handler servlet).
*
* @author Sven Strickroth
* @author Ceki G&uuml;lc&uuml;
*/
public class MDCInsertingServletRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
insertIntoMDC(sre.getServletRequest());
}

@Override
public void requestDestroyed(ServletRequestEvent sre) {
clearMDC();
}

private void insertIntoMDC(final ServletRequest request) {
MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());

if (request instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
StringBuffer requestURL = httpServletRequest.getRequestURL();
if (requestURL != null) {
MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
}
MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
}
}

private void clearMDC() {
MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
// removing possibly nonexistent item is OK
MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
MDC.remove(ClassicConstants.REQUEST_METHOD);
MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, 2024, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.helpers;

import java.security.Principal;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.MDC;

/**
* A simple servlet request listener that stores the username
* found in the Principal in the MDC.
*
* <p> The value is removed from the MDC once the request has
* been fully processed (including an error handler servlet).
*
* @author Sven Strickroth
* @author S&eacute;bastien Pennec
*/
@WebListener
public class UserServletRequestListener implements ServletRequestListener {

private final String USER_KEY = "username";

@Override
public void requestDestroyed(ServletRequestEvent sre) {
MDC.clear();
}

@Override
public void requestInitialized(ServletRequestEvent sre) {
if (sre.getServletRequest() instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
Principal principal = request.getUserPrincipal();
if (principal != null) {
String username = principal.getName();
registerUsername(username);
}
}
}

private void registerUsername(final String username) {
if (username != null && !username.trim().isEmpty()) {
MDC.put(USER_KEY, username);
}
}
}

0 comments on commit f1ba14a

Please sign in to comment.