Updated Code for logging.

This commit is contained in:
piyushkag
2024-11-20 15:58:31 +05:30
parent f66ddf6e47
commit 18e970219d
2 changed files with 30 additions and 16 deletions

View File

@@ -15,9 +15,14 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.servlet.http.HttpServletRequest;
import net.gepafin.tendermanagement.constants.GepafinConstant;
import org.apache.commons.collections4.MapUtils;
@@ -517,19 +522,30 @@ public class Utils {
public static String convertEntityToJsonForLogging(Object entity) {
if(entity == null){
if (entity == null) {
return null;
}
try {
return mapper.writeValueAsString(entity);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
@JsonIgnoreProperties({ "childEntities", "relatedData", "otherRelations" })
private abstract static class IgnoreChildEntities {
try {
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
JsonNode entityNode = mapper.valueToTree(entity);
ObjectNode objectNode = (ObjectNode) entityNode;
Field[] fields = entity.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getType().isAnnotationPresent(ManyToOne.class) || field.getType().isAnnotationPresent(OneToMany.class) || field.getType()
.isAnnotationPresent(OneToOne.class) || field.getType().isAnnotationPresent(ManyToMany.class)) {
objectNode.remove(field.getName());
}
}
return mapper.writeValueAsString(objectNode);
} catch (JsonProcessingException e) {
log.error("Failed to parse entity in json {}", e.getMessage());
return null;
}
}
public static <T> T getClonedEntityForData(T entity) {
@@ -542,8 +558,8 @@ public class Utils {
@SuppressWarnings("unchecked") T clonedEntity = (T) mapper.readValue(json, entity.getClass());
return clonedEntity;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to clone entity", e);
log.error("Failed to clone entity {}", e.getMessage());
return null;
}
}
}