|
| 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ülcü |
| 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 | +} |
0 commit comments