Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Author

@csware csware Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we could also call the insertIntoMDC in the MDCInsertingServletFilter to avoid code duplication.

}

@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);
}
}
}