From c7115f752af298be01dca2fe1708a1d07a1e0ca7 Mon Sep 17 00:00:00 2001 From: Ioannis Kalyvas Date: Tue, 16 Oct 2018 18:13:02 +0300 Subject: [PATCH] Refactoring Elastic Search Loggers for Logging and Auditing --- .../java/eu/eudat/core/logger/Logger.java | 1 + .../logger/remote/http/HttpRemoteLogger.java | 100 +++++++++++++----- .../exception/ApiExceptionLoggingModel.java | 2 +- .../exception/ExceptionLoggingModel.java | 4 +- .../ControllerErrorHandler.java | 18 +++- .../utilities/InvitationServiceImpl.java | 10 +- 6 files changed, 94 insertions(+), 41 deletions(-) diff --git a/dmp-backend/logging/src/main/java/eu/eudat/core/logger/Logger.java b/dmp-backend/logging/src/main/java/eu/eudat/core/logger/Logger.java index a89dc397e..86e5c74e1 100644 --- a/dmp-backend/logging/src/main/java/eu/eudat/core/logger/Logger.java +++ b/dmp-backend/logging/src/main/java/eu/eudat/core/logger/Logger.java @@ -1,5 +1,6 @@ package eu.eudat.core.logger; +import com.fasterxml.jackson.core.JsonProcessingException; import eu.eudat.core.models.exception.ApiExceptionLoggingModel; /** diff --git a/dmp-backend/logging/src/main/java/eu/eudat/core/logger/remote/http/HttpRemoteLogger.java b/dmp-backend/logging/src/main/java/eu/eudat/core/logger/remote/http/HttpRemoteLogger.java index 068606635..774fdff9a 100644 --- a/dmp-backend/logging/src/main/java/eu/eudat/core/logger/remote/http/HttpRemoteLogger.java +++ b/dmp-backend/logging/src/main/java/eu/eudat/core/logger/remote/http/HttpRemoteLogger.java @@ -1,5 +1,8 @@ package eu.eudat.core.logger.remote.http; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import eu.eudat.core.logger.Logger; import eu.eudat.core.logger.common.AbstractBatchLogger; import eu.eudat.core.models.LoggingModel; @@ -88,9 +91,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void debug(T exception) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setType(LoggingType.DEBUG); this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); @@ -98,9 +107,16 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void debug(T exception, String message) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + exceptionLoggingModel.setData(map); exceptionLoggingModel.setMessage(message); exceptionLoggingModel.setType(LoggingType.DEBUG); @@ -134,9 +150,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void warn(T exception) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setType(LoggingType.WARNING); this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); @@ -144,9 +166,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void warn(T exception, String message) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setMessage(message); exceptionLoggingModel.setType(LoggingType.WARNING); @@ -180,9 +208,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void info(T exception) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setType(LoggingType.INFO); this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); @@ -190,9 +224,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void info(T exception, String message) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setMessage(message); exceptionLoggingModel.setType(LoggingType.INFO); @@ -214,9 +254,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void error(T exception) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setType(LoggingType.ERROR); this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel); @@ -224,9 +270,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger { @Override public void error(T exception, String message) { - ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel<>(); - Map map = new HashMap<>(); - map.put("exception", exception); + ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel(); + Map map = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(exception); + map.put("exception", json); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } exceptionLoggingModel.setData(map); exceptionLoggingModel.setMessage(message); exceptionLoggingModel.setType(LoggingType.ERROR); diff --git a/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ApiExceptionLoggingModel.java b/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ApiExceptionLoggingModel.java index d1ca09db4..d8991a967 100644 --- a/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ApiExceptionLoggingModel.java +++ b/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ApiExceptionLoggingModel.java @@ -8,7 +8,7 @@ import java.util.Map; /** * Created by ikalyvas on 6/12/2018. */ -public class ApiExceptionLoggingModel extends LoggingModel> { +public class ApiExceptionLoggingModel extends LoggingModel { private HttpStatus code; private P user; private final String indexType = "api-exception-logging"; diff --git a/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ExceptionLoggingModel.java b/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ExceptionLoggingModel.java index 79cbf283f..815c8bfed 100644 --- a/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ExceptionLoggingModel.java +++ b/dmp-backend/logging/src/main/java/eu/eudat/core/models/exception/ExceptionLoggingModel.java @@ -7,7 +7,7 @@ import java.util.Map; /** * Created by ikalyvas on 5/30/2018. */ -public class ExceptionLoggingModel extends LoggingModel> { +public class ExceptionLoggingModel extends LoggingModel> { private final String indexType = "exception-logging"; public String getIndexType() { @@ -15,7 +15,7 @@ public class ExceptionLoggingModel extends LoggingModel getData() { + public Map getData() { return super.getData(); } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/controllerhandler/ControllerErrorHandler.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/controllerhandler/ControllerErrorHandler.java index 3da81d2d4..c7fdde3b1 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/controllerhandler/ControllerErrorHandler.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/controllerhandler/ControllerErrorHandler.java @@ -1,12 +1,14 @@ package eu.eudat.controllers.controllerhandler; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import eu.eudat.core.logger.Logger; import eu.eudat.core.models.exception.ApiExceptionLoggingModel; import eu.eudat.models.data.helpers.responses.ResponseItem; import eu.eudat.models.data.security.Principal; import eu.eudat.types.ApiMessageCode; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -36,12 +38,18 @@ public class ControllerErrorHandler { @ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseBody public ResponseItem processValidationError(Principal principal, Exception ex) throws Exception { - ApiExceptionLoggingModel apiExceptionLoggingModel = new ApiExceptionLoggingModel<>(); + ApiExceptionLoggingModel apiExceptionLoggingModel = new ApiExceptionLoggingModel<>(); apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST); apiExceptionLoggingModel.setUser(principal); - Map exceptionMap = new HashMap<>(); - exceptionMap.put("exception", ex); - apiExceptionLoggingModel.setData(exceptionMap); + Map exceptionMap = new HashMap<>(); + ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); + try { + String json = ow.writeValueAsString(ex); + exceptionMap.put("exception", json); + apiExceptionLoggingModel.setData(ow.writeValueAsString(exceptionMap)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } apiExceptionLoggingModel.setMessage(ex.getMessage()); apiExceptionLoggingModel.setType(LoggingType.ERROR); ex.printStackTrace(); diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/services/utilities/InvitationServiceImpl.java b/dmp-backend/web/src/main/java/eu/eudat/logic/services/utilities/InvitationServiceImpl.java index 9e974e419..9edb04eda 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/services/utilities/InvitationServiceImpl.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/services/utilities/InvitationServiceImpl.java @@ -75,16 +75,8 @@ public class InvitationServiceImpl implements InvitationService { try { mailService.sendSimpleMail(mail); } catch (Exception ex) { - ApiExceptionLoggingModel apiExceptionLoggingModel = new ApiExceptionLoggingModel<>(); - apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST); - apiExceptionLoggingModel.setUser(invitation.getUser()); - Map exceptionMap = new HashMap<>(); - exceptionMap.put("exception", ex); - apiExceptionLoggingModel.setData(exceptionMap); - apiExceptionLoggingModel.setMessage(ex.getMessage()); - apiExceptionLoggingModel.setType(LoggingType.ERROR); ex.printStackTrace(); - this.logger.error(apiExceptionLoggingModel); + this.logger.error(ex, ex.getMessage()); } }); }