From 4d265b8acb4736b57d816ca6f35601b32114696b Mon Sep 17 00:00:00 2001 From: catbref Date: Tue, 2 Jul 2019 15:13:46 +0100 Subject: [PATCH] Don't log API exceptions or errors if Settings.isApiLoggingEnabled is false --- .../java/org/qora/api/ApiErrorHandler.java | 21 +++++++++++-------- .../qora/api/resource/ApiExceptionMapper.java | 4 +++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/qora/api/ApiErrorHandler.java b/src/main/java/org/qora/api/ApiErrorHandler.java index de30ed83..b5d4fb35 100644 --- a/src/main/java/org/qora/api/ApiErrorHandler.java +++ b/src/main/java/org/qora/api/ApiErrorHandler.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.ErrorHandler; +import org.qora.settings.Settings; public class ApiErrorHandler extends ErrorHandler { @@ -17,17 +18,19 @@ public class ApiErrorHandler extends ErrorHandler { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { - String requestURI = request.getRequestURI(); + if (Settings.getInstance().isApiLoggingEnabled()) { + String requestURI = request.getRequestURI(); - String queryString = request.getQueryString(); - if (queryString != null) - requestURI += "?" + queryString; + String queryString = request.getQueryString(); + if (queryString != null) + requestURI += "?" + queryString; - Throwable th = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); - if (th != null) { - LOGGER.error(String.format("Unexpected %s during request %s", th.getClass().getCanonicalName(), requestURI)); - } else { - LOGGER.error(String.format("Unexpected error during request %s", requestURI)); + Throwable th = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); + if (th != null) { + LOGGER.error(String.format("Unexpected %s during request %s", th.getClass().getCanonicalName(), requestURI)); + } else { + LOGGER.error(String.format("Unexpected error during request %s", requestURI)); + } } super.handle(target, baseRequest, request, response); diff --git a/src/main/java/org/qora/api/resource/ApiExceptionMapper.java b/src/main/java/org/qora/api/resource/ApiExceptionMapper.java index dd24c1f7..150e11af 100644 --- a/src/main/java/org/qora/api/resource/ApiExceptionMapper.java +++ b/src/main/java/org/qora/api/resource/ApiExceptionMapper.java @@ -11,6 +11,7 @@ import javax.ws.rs.ext.Provider; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.glassfish.jersey.server.ParamException; +import org.qora.settings.Settings; @Provider public class ApiExceptionMapper implements ExceptionMapper { @@ -25,7 +26,8 @@ public class ApiExceptionMapper implements ExceptionMapper { @Override public Response toResponse(RuntimeException e) { - LOGGER.info(String.format("Exception %s during API call: %s", e.getClass().getCanonicalName(), request.getRequestURI())); + if (Settings.getInstance().isApiLoggingEnabled()) + LOGGER.info(String.format("Exception %s during API call: %s", e.getClass().getCanonicalName(), request.getRequestURI())); if (e instanceof ParamException) { ParamException pe = (ParamException) e;