diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletRequestListener.java b/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletRequestListener.java new file mode 100644 index 0000000000..a16b29eab5 --- /dev/null +++ b/logback-classic/src/main/java/ch/qos/logback/classic/helpers/MDCInsertingServletRequestListener.java @@ -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 + * + *

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); + } +} diff --git a/logback-examples/src/main/java/chapters/mdc/UserServletRequestListener.java b/logback-examples/src/main/java/chapters/mdc/UserServletRequestListener.java new file mode 100644 index 0000000000..8f2bae5df1 --- /dev/null +++ b/logback-examples/src/main/java/chapters/mdc/UserServletRequestListener.java @@ -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. + * + *

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); + } + } +}