-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cc287a
commit faa2ca7
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package uk.gov.beis.els.mvc; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Component | ||
public class RequestLogFilter extends OncePerRequestFilter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(RequestLogFilter.class); | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
|
||
var startTimeMs = System.currentTimeMillis(); | ||
|
||
try { | ||
filterChain.doFilter(request, response); | ||
} finally { | ||
var queryString = request.getQueryString() == null ? "" : request.getQueryString(); | ||
if (!queryString.isEmpty()) { | ||
queryString = "?" + queryString; | ||
} | ||
|
||
LOGGER.info( | ||
"Request: {} {}{}, time: {}, status: {}", | ||
request.getMethod(), | ||
request.getRequestURI(), | ||
queryString, | ||
System.currentTimeMillis() - startTimeMs, | ||
response.getStatus() | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
protected boolean shouldNotFilter(HttpServletRequest request) { | ||
var requestUri = request.getRequestURI(); | ||
return requestUri.startsWith("/actuator") | ||
|| requestUri.startsWith("/assets"); | ||
} | ||
} |