|
| 1 | +package com.mongodb.samplemflix.config; |
| 2 | + |
| 3 | +import jakarta.servlet.FilterChain; |
| 4 | +import jakarta.servlet.ServletException; |
| 5 | +import jakarta.servlet.http.HttpServletRequest; |
| 6 | +import jakarta.servlet.http.HttpServletResponse; |
| 7 | +import java.io.IOException; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import org.springframework.core.Ordered; |
| 11 | +import org.springframework.core.annotation.Order; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 14 | + |
| 15 | +/** |
| 16 | + * HTTP Request Logging Filter |
| 17 | + * |
| 18 | + * <p>This filter logs all incoming HTTP requests with useful information |
| 19 | + * including method, URL, status code, and response time. |
| 20 | + * It helps with debugging and monitoring application traffic. |
| 21 | + * |
| 22 | + * <p>Log output format: |
| 23 | + * <pre> |
| 24 | + * INFO - GET /api/movies 200 - 45ms |
| 25 | + * WARN - GET /api/movies/invalid 400 - 2ms |
| 26 | + * ERROR - POST /api/movies 500 - 120ms |
| 27 | + * </pre> |
| 28 | + * |
| 29 | + * <p>The filter is ordered to run first in the filter chain to ensure |
| 30 | + * accurate timing measurements. |
| 31 | + */ |
| 32 | +@Component |
| 33 | +@Order(Ordered.HIGHEST_PRECEDENCE) |
| 34 | +public class RequestLoggingFilter extends OncePerRequestFilter { |
| 35 | + |
| 36 | + private static final Logger logger = LoggerFactory.getLogger(RequestLoggingFilter.class); |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void doFilterInternal( |
| 40 | + HttpServletRequest request, |
| 41 | + HttpServletResponse response, |
| 42 | + FilterChain filterChain) throws ServletException, IOException { |
| 43 | + |
| 44 | + // Record the start time |
| 45 | + long startTime = System.currentTimeMillis(); |
| 46 | + |
| 47 | + // Log incoming request at debug level |
| 48 | + logger.debug("Incoming request: {} {} from {}", |
| 49 | + request.getMethod(), |
| 50 | + request.getRequestURI(), |
| 51 | + request.getRemoteAddr()); |
| 52 | + |
| 53 | + try { |
| 54 | + // Continue with the filter chain |
| 55 | + filterChain.doFilter(request, response); |
| 56 | + } finally { |
| 57 | + // Calculate response time |
| 58 | + long responseTime = System.currentTimeMillis() - startTime; |
| 59 | + |
| 60 | + // Log the completed request with appropriate level based on status code |
| 61 | + logRequest(request.getMethod(), request.getRequestURI(), response.getStatus(), responseTime); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Logs the HTTP request with appropriate log level based on status code. |
| 67 | + * |
| 68 | + * <p>Log levels: |
| 69 | + * <ul> |
| 70 | + * <li>ERROR: 5xx server errors</li> |
| 71 | + * <li>WARN: 4xx client errors</li> |
| 72 | + * <li>INFO: 2xx and 3xx success/redirect</li> |
| 73 | + * </ul> |
| 74 | + * |
| 75 | + * @param method HTTP method (GET, POST, etc.) |
| 76 | + * @param uri Request URI |
| 77 | + * @param statusCode HTTP response status code |
| 78 | + * @param responseTime Response time in milliseconds |
| 79 | + */ |
| 80 | + private void logRequest(String method, String uri, int statusCode, long responseTime) { |
| 81 | + String message = String.format("%s %s %d - %dms", method, uri, statusCode, responseTime); |
| 82 | + |
| 83 | + if (statusCode >= 500) { |
| 84 | + logger.error(message); |
| 85 | + } else if (statusCode >= 400) { |
| 86 | + logger.warn(message); |
| 87 | + } else { |
| 88 | + logger.info(message); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Skip logging for static resources and health checks to reduce noise. |
| 94 | + */ |
| 95 | + @Override |
| 96 | + protected boolean shouldNotFilter(HttpServletRequest request) { |
| 97 | + String path = request.getRequestURI(); |
| 98 | + return path.startsWith("/swagger-ui") |
| 99 | + || path.startsWith("/api-docs") |
| 100 | + || path.startsWith("/v3/api-docs") |
| 101 | + || path.equals("/favicon.ico") |
| 102 | + || path.startsWith("/actuator"); |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments