Don't log API exceptions or errors if Settings.isApiLoggingEnabled is false

This commit is contained in:
catbref 2019-07-02 15:13:46 +01:00
parent a3e6c24a89
commit 4d265b8acb
2 changed files with 15 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.ErrorHandler;
import org.qora.settings.Settings;
public class ApiErrorHandler extends ErrorHandler { public class ApiErrorHandler extends ErrorHandler {
@ -17,6 +18,7 @@ public class ApiErrorHandler extends ErrorHandler {
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (Settings.getInstance().isApiLoggingEnabled()) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
String queryString = request.getQueryString(); String queryString = request.getQueryString();
@ -29,6 +31,7 @@ public class ApiErrorHandler extends ErrorHandler {
} else { } else {
LOGGER.error(String.format("Unexpected error during request %s", requestURI)); LOGGER.error(String.format("Unexpected error during request %s", requestURI));
} }
}
super.handle(target, baseRequest, request, response); super.handle(target, baseRequest, request, response);
} }

View File

@ -11,6 +11,7 @@ import javax.ws.rs.ext.Provider;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.server.ParamException; import org.glassfish.jersey.server.ParamException;
import org.qora.settings.Settings;
@Provider @Provider
public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> { public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> {
@ -25,6 +26,7 @@ public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> {
@Override @Override
public Response toResponse(RuntimeException e) { public Response toResponse(RuntimeException e) {
if (Settings.getInstance().isApiLoggingEnabled())
LOGGER.info(String.format("Exception %s during API call: %s", e.getClass().getCanonicalName(), request.getRequestURI())); LOGGER.info(String.format("Exception %s during API call: %s", e.getClass().getCanonicalName(), request.getRequestURI()));
if (e instanceof ParamException) { if (e instanceof ParamException) {