Skip to content

Commit

Permalink
feat: 🎸 Add logs when errors (#925)
Browse files Browse the repository at this point in the history
Specifically for the runtime exception, there should be more logs
  • Loading branch information
mattboll authored Dec 6, 2024
1 parent 0c4962c commit 5e6e447
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=80MB
spring.profiles.active=
spring.liquibase.change-log=classpath:db/changelog/databaseChangeLog.xml
server.error.include-stacktrace=never
server.error.include-stacktrace=always
rabbitmq.exchange.pdf.generator=exchange.pdf.generator
rabbitmq.routing.key.pdf.generator.watermark-document=routing.key.pdf.generator.watermark-document
rabbitmq.routing.key.pdf.generator.apartment-sharing=routing.key.pdf.generator.apartment-sharing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public abstract class AbstractConnectionContextFilter extends HttpFilter {
private static final String REQUEST_ID = "request_id";
private static final String RESPONSE_STATUS = "response_status";
private static final String REAL_IP = "ip";
private static final String JAKARTA_SERVLET_FORWARD_REQUEST_URI = "jakarta.servlet.forward.request_uri";
private static final String JAKARTA_SERVLET_ERROR_MESSAGE = "jakarta.servlet.error.message";
private static final String JAKARTA_SERVLET_ERROR_EXCEPTION = "jakarta.servlet.error.exception";

@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
Expand All @@ -34,7 +37,16 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response

getAdditionalContextElements().forEach(MDC::put);

log.info("Request: method={}, path={}", request.getMethod(), request.getRequestURI());
String requestUri = request.getRequestURI();
if ("/error".equals(requestUri) && request.getAttribute(JAKARTA_SERVLET_FORWARD_REQUEST_URI) != null) {
String previousRequestUri = request.getAttribute(JAKARTA_SERVLET_FORWARD_REQUEST_URI).toString();
Object errorMessage = request.getAttribute(JAKARTA_SERVLET_ERROR_MESSAGE);
Object errorException = request.getAttribute(JAKARTA_SERVLET_ERROR_EXCEPTION);
Object bestMatchingHandler = request.getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingHandler");
log.info("Request: method={}, path={}, previousRequestUri={}, errorMessage={}, errorException={}, bestMatchingHandler={}", request.getMethod(), requestUri, previousRequestUri, errorMessage, errorException, bestMatchingHandler);
} else {
log.info("Request: method={}, path={}", request.getMethod(), requestUri);
}
} catch (Exception e) {
// Something wrong but service should stay up
log.warn("Unable to inject data in MDC", e);
Expand All @@ -45,7 +57,10 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
} finally {
if (response.getStatus() != 200) {
MDC.put(RESPONSE_STATUS, String.valueOf(response.getStatus()));
if (response.getStatus() < 400) {
if ("/error".equals(request.getRequestURI()) && request.getAttribute(JAKARTA_SERVLET_FORWARD_REQUEST_URI) != null) {
String previousRequestUri = request.getAttribute(JAKARTA_SERVLET_FORWARD_REQUEST_URI).toString();
log.info("Response: status={}, method={}, path={}, previousRequestUri={}", response.getStatus(), request.getMethod(), request.getRequestURI(), previousRequestUri);
} else if (response.getStatus() < 400) {
log.info("Response: status={}, method={}, path={}", response.getStatus(), request.getMethod(), request.getRequestURI());
} else if (response.getStatus() < 500) {
log.warn("Response: status={}, method={}, path={}", response.getStatus(), request.getMethod(), request.getRequestURI());
Expand Down

0 comments on commit 5e6e447

Please sign in to comment.