Refactoring Elastic Search Loggers for Logging and Auditing

This commit is contained in:
Ioannis Kalyvas 2018-10-16 18:13:02 +03:00
parent 01de2754e5
commit c7115f752a
6 changed files with 94 additions and 41 deletions

View File

@ -1,5 +1,6 @@
package eu.eudat.core.logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
/**

View File

@ -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 <T extends Exception> void debug(T exception) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends Exception> void debug(T exception, String message) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends Exception> void warn(T exception) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends Exception> void warn(T exception, String message) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends RuntimeException> void info(T exception) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends RuntimeException> void info(T exception, String message) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends Exception> void error(T exception) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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 <T extends Exception> void error(T exception, String message) {
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
Map<String, T> map = new HashMap<>();
map.put("exception", exception);
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
Map<String, String> 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);

View File

@ -8,7 +8,7 @@ import java.util.Map;
/**
* Created by ikalyvas on 6/12/2018.
*/
public class ApiExceptionLoggingModel<E extends Exception,P> extends LoggingModel<Map<String,E>> {
public class ApiExceptionLoggingModel<E, P> extends LoggingModel<E> {
private HttpStatus code;
private P user;
private final String indexType = "api-exception-logging";

View File

@ -7,7 +7,7 @@ import java.util.Map;
/**
* Created by ikalyvas on 5/30/2018.
*/
public class ExceptionLoggingModel<E extends Exception> extends LoggingModel<Map<String, E>> {
public class ExceptionLoggingModel extends LoggingModel<Map<String, String>> {
private final String indexType = "exception-logging";
public String getIndexType() {
@ -15,7 +15,7 @@ public class ExceptionLoggingModel<E extends Exception> extends LoggingModel<Map
}
@Override
public Map<String, E> getData() {
public Map<String, String> getData() {
return super.getData();
}
}

View File

@ -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<Exception> processValidationError(Principal principal, Exception ex) throws Exception {
ApiExceptionLoggingModel<Exception, Principal> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
ApiExceptionLoggingModel<String, Principal> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST);
apiExceptionLoggingModel.setUser(principal);
Map<String, Exception> exceptionMap = new HashMap<>();
exceptionMap.put("exception", ex);
apiExceptionLoggingModel.setData(exceptionMap);
Map<String, String> 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();

View File

@ -75,16 +75,8 @@ public class InvitationServiceImpl implements InvitationService {
try {
mailService.sendSimpleMail(mail);
} catch (Exception ex) {
ApiExceptionLoggingModel<Exception, UserInfo> apiExceptionLoggingModel = new ApiExceptionLoggingModel<>();
apiExceptionLoggingModel.setCode(HttpStatus.BAD_REQUEST);
apiExceptionLoggingModel.setUser(invitation.getUser());
Map<String, Exception> 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());
}
});
}