Skip to content

Commit

Permalink
Add request log
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbarnett91 committed Dec 19, 2023
1 parent 7cc287a commit faa2ca7
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/uk/gov/beis/els/mvc/RequestLogFilter.java
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");
}
}

0 comments on commit faa2ca7

Please sign in to comment.