Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Add logs when errors #925

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading