-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide servlet request listener which fill the MDC
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
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...ssic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletRequestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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ülcü | ||
*/ | ||
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); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
logback-examples/src/main/java/chapters/mdc/UserServletRequestListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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é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); | ||
} | ||
} | ||
} |