-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Provide servlet request listener which fill the MDC #769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
csware
wants to merge
1
commit into
qos-ch:master
Choose a base branch
from
csware:mdc-servlet-request-listener
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 theMDCInsertingServletFilter
to avoid code duplication.