Skip to content

Commit 52818a8

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 52818a8

File tree

2 files changed

+137
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)