Skip to content

Commit f1ba14a

Browse files
committed
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]>
1 parent f3efb8f commit f1ba14a

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2015, 2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
package ch.qos.logback.classic.helpers;
15+
16+
import javax.servlet.ServletRequest;
17+
import javax.servlet.ServletRequestEvent;
18+
import javax.servlet.ServletRequestListener;
19+
import javax.servlet.http.HttpServletRequest;
20+
21+
import org.slf4j.MDC;
22+
23+
import ch.qos.logback.classic.ClassicConstants;
24+
25+
/**
26+
* A simple servlet request listener that stores the remote address,
27+
* used HTTP method, and
28+
*
29+
* <p> The value is removed from the MDC once the request has
30+
* been fully processed (including an error handler servlet).
31+
*
32+
* @author Sven Strickroth
33+
* @author Ceki G&uuml;lc&uuml;
34+
*/
35+
public class MDCInsertingServletRequestListener implements ServletRequestListener {
36+
@Override
37+
public void requestInitialized(ServletRequestEvent sre) {
38+
insertIntoMDC(sre.getServletRequest());
39+
}
40+
41+
@Override
42+
public void requestDestroyed(ServletRequestEvent sre) {
43+
clearMDC();
44+
}
45+
46+
private void insertIntoMDC(final ServletRequest request) {
47+
MDC.put(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY, request.getRemoteHost());
48+
49+
if (request instanceof HttpServletRequest) {
50+
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
51+
MDC.put(ClassicConstants.REQUEST_REQUEST_URI, httpServletRequest.getRequestURI());
52+
StringBuffer requestURL = httpServletRequest.getRequestURL();
53+
if (requestURL != null) {
54+
MDC.put(ClassicConstants.REQUEST_REQUEST_URL, requestURL.toString());
55+
}
56+
MDC.put(ClassicConstants.REQUEST_METHOD, httpServletRequest.getMethod());
57+
MDC.put(ClassicConstants.REQUEST_QUERY_STRING, httpServletRequest.getQueryString());
58+
MDC.put(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY, httpServletRequest.getHeader("User-Agent"));
59+
MDC.put(ClassicConstants.REQUEST_X_FORWARDED_FOR, httpServletRequest.getHeader("X-Forwarded-For"));
60+
}
61+
}
62+
63+
private void clearMDC() {
64+
MDC.remove(ClassicConstants.REQUEST_REMOTE_HOST_MDC_KEY);
65+
MDC.remove(ClassicConstants.REQUEST_REQUEST_URI);
66+
MDC.remove(ClassicConstants.REQUEST_QUERY_STRING);
67+
// removing possibly nonexistent item is OK
68+
MDC.remove(ClassicConstants.REQUEST_REQUEST_URL);
69+
MDC.remove(ClassicConstants.REQUEST_METHOD);
70+
MDC.remove(ClassicConstants.REQUEST_USER_AGENT_MDC_KEY);
71+
MDC.remove(ClassicConstants.REQUEST_X_FORWARDED_FOR);
72+
}
73+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2015, 2024, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
package ch.qos.logback.classic.helpers;
15+
16+
import java.security.Principal;
17+
18+
import javax.servlet.ServletRequestEvent;
19+
import javax.servlet.ServletRequestListener;
20+
import javax.servlet.annotation.WebListener;
21+
import javax.servlet.http.HttpServletRequest;
22+
23+
import org.slf4j.MDC;
24+
25+
/**
26+
* A simple servlet request listener that stores the username
27+
* found in the Principal in the MDC.
28+
*
29+
* <p> The value is removed from the MDC once the request has
30+
* been fully processed (including an error handler servlet).
31+
*
32+
* @author Sven Strickroth
33+
* @author S&eacute;bastien Pennec
34+
*/
35+
@WebListener
36+
public class UserServletRequestListener implements ServletRequestListener {
37+
38+
private final String USER_KEY = "username";
39+
40+
@Override
41+
public void requestDestroyed(ServletRequestEvent sre) {
42+
MDC.clear();
43+
}
44+
45+
@Override
46+
public void requestInitialized(ServletRequestEvent sre) {
47+
if (sre.getServletRequest() instanceof HttpServletRequest) {
48+
HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
49+
Principal principal = request.getUserPrincipal();
50+
if (principal != null) {
51+
String username = principal.getName();
52+
registerUsername(username);
53+
}
54+
}
55+
}
56+
57+
private void registerUsername(final String username) {
58+
if (username != null && !username.trim().isEmpty()) {
59+
MDC.put(USER_KEY, username);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)