Refactoring Elastic Search Loggers for Logging and Auditing
This commit is contained in:
parent
01de2754e5
commit
c7115f752a
|
@ -1,5 +1,6 @@
|
||||||
package eu.eudat.core.logger;
|
package eu.eudat.core.logger;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
|
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package eu.eudat.core.logger.remote.http;
|
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.Logger;
|
||||||
import eu.eudat.core.logger.common.AbstractBatchLogger;
|
import eu.eudat.core.logger.common.AbstractBatchLogger;
|
||||||
import eu.eudat.core.models.LoggingModel;
|
import eu.eudat.core.models.LoggingModel;
|
||||||
|
@ -88,9 +91,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void debug(T exception) {
|
public <T extends Exception> void debug(T exception) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setType(LoggingType.DEBUG);
|
exceptionLoggingModel.setType(LoggingType.DEBUG);
|
||||||
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
||||||
|
@ -98,9 +107,16 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void debug(T exception, String message) {
|
public <T extends Exception> void debug(T exception, String message) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setMessage(message);
|
exceptionLoggingModel.setMessage(message);
|
||||||
exceptionLoggingModel.setType(LoggingType.DEBUG);
|
exceptionLoggingModel.setType(LoggingType.DEBUG);
|
||||||
|
@ -134,9 +150,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void warn(T exception) {
|
public <T extends Exception> void warn(T exception) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setType(LoggingType.WARNING);
|
exceptionLoggingModel.setType(LoggingType.WARNING);
|
||||||
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
||||||
|
@ -144,9 +166,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void warn(T exception, String message) {
|
public <T extends Exception> void warn(T exception, String message) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setMessage(message);
|
exceptionLoggingModel.setMessage(message);
|
||||||
exceptionLoggingModel.setType(LoggingType.WARNING);
|
exceptionLoggingModel.setType(LoggingType.WARNING);
|
||||||
|
@ -180,9 +208,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends RuntimeException> void info(T exception) {
|
public <T extends RuntimeException> void info(T exception) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setType(LoggingType.INFO);
|
exceptionLoggingModel.setType(LoggingType.INFO);
|
||||||
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
||||||
|
@ -190,9 +224,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends RuntimeException> void info(T exception, String message) {
|
public <T extends RuntimeException> void info(T exception, String message) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setMessage(message);
|
exceptionLoggingModel.setMessage(message);
|
||||||
exceptionLoggingModel.setType(LoggingType.INFO);
|
exceptionLoggingModel.setType(LoggingType.INFO);
|
||||||
|
@ -214,9 +254,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void error(T exception) {
|
public <T extends Exception> void error(T exception) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setType(LoggingType.ERROR);
|
exceptionLoggingModel.setType(LoggingType.ERROR);
|
||||||
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
this.put(String.valueOf(exception.hashCode()), exceptionLoggingModel);
|
||||||
|
@ -224,9 +270,15 @@ public class HttpRemoteLogger extends AbstractBatchLogger implements Logger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Exception> void error(T exception, String message) {
|
public <T extends Exception> void error(T exception, String message) {
|
||||||
ExceptionLoggingModel<T> exceptionLoggingModel = new ExceptionLoggingModel<>();
|
ExceptionLoggingModel exceptionLoggingModel = new ExceptionLoggingModel();
|
||||||
Map<String, T> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("exception", exception);
|
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.setData(map);
|
||||||
exceptionLoggingModel.setMessage(message);
|
exceptionLoggingModel.setMessage(message);
|
||||||
exceptionLoggingModel.setType(LoggingType.ERROR);
|
exceptionLoggingModel.setType(LoggingType.ERROR);
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.Map;
|
||||||
/**
|
/**
|
||||||
* Created by ikalyvas on 6/12/2018.
|
* 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 HttpStatus code;
|
||||||
private P user;
|
private P user;
|
||||||
private final String indexType = "api-exception-logging";
|
private final String indexType = "api-exception-logging";
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.util.Map;
|
||||||
/**
|
/**
|
||||||
* Created by ikalyvas on 5/30/2018.
|
* 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";
|
private final String indexType = "exception-logging";
|
||||||
|
|
||||||
public String getIndexType() {
|
public String getIndexType() {
|
||||||
|
@ -15,7 +15,7 @@ public class ExceptionLoggingModel<E extends Exception> extends LoggingModel<Map
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, E> getData() {
|
public Map<String, String> getData() {
|
||||||
return super.getData();
|
return super.getData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
package eu.eudat.controllers.controllerhandler;
|
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.logger.Logger;
|
||||||
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
|
import eu.eudat.core.models.exception.ApiExceptionLoggingModel;
|
||||||
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
||||||
import eu.eudat.models.data.security.Principal;
|
import eu.eudat.models.data.security.Principal;
|
||||||
import eu.eudat.types.ApiMessageCode;
|
import eu.eudat.types.ApiMessageCode;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
@ -36,12 +38,18 @@ public class ControllerErrorHandler {
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseItem<Exception> processValidationError(Principal principal, Exception ex) throws Exception {
|
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.setCode(HttpStatus.BAD_REQUEST);
|
||||||
apiExceptionLoggingModel.setUser(principal);
|
apiExceptionLoggingModel.setUser(principal);
|
||||||
Map<String, Exception> exceptionMap = new HashMap<>();
|
Map<String, String> exceptionMap = new HashMap<>();
|
||||||
exceptionMap.put("exception", ex);
|
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
||||||
apiExceptionLoggingModel.setData(exceptionMap);
|
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.setMessage(ex.getMessage());
|
||||||
apiExceptionLoggingModel.setType(LoggingType.ERROR);
|
apiExceptionLoggingModel.setType(LoggingType.ERROR);
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
|
|
|
@ -75,16 +75,8 @@ public class InvitationServiceImpl implements InvitationService {
|
||||||
try {
|
try {
|
||||||
mailService.sendSimpleMail(mail);
|
mailService.sendSimpleMail(mail);
|
||||||
} catch (Exception ex) {
|
} 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();
|
ex.printStackTrace();
|
||||||
this.logger.error(apiExceptionLoggingModel);
|
this.logger.error(ex, ex.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue