Remove excessive logs in LogUtils (#7228)

Co-authored-by: SadiJr <sadi@scclouds.com.br>
This commit is contained in:
SadiJr 2023-06-07 04:48:52 -03:00 committed by GitHub
parent d93fe91364
commit 9cb561e03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 3 deletions

View File

@ -28,6 +28,7 @@ import java.util.Set;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
@ -78,21 +79,29 @@ public class LogUtils {
return fileNames;
}
/**
* Tries to convert message parameters to JSON format and use them in the message.
* @param formatMessage message to format.
* @param objects objects to convert to JSON. A null object will be defaulted to the String "null";
* if it is not possible to convert an object to JSON, the object's 'toString' will be used instead.
* @return the formatted message.
*/
public static String logGsonWithoutException(String formatMessage, Object ... objects) {
List<String> gsons = new ArrayList<>();
for (Object object : objects) {
try {
gsons.add(GSON.toJson(object));
} catch (Exception e) {
LOGGER.debug(String.format("Failed to log object [%s] using GSON.", object != null ? object.getClass().getSimpleName() : "null"));
gsons.add("error to decode");
Object errObj = ObjectUtils.defaultIfNull(object, "null");
LOGGER.trace(String.format("Failed to log object [%s] using GSON.", errObj.getClass().getSimpleName()));
gsons.add("error decoding " + errObj);
}
}
try {
return String.format(formatMessage, gsons.toArray());
} catch (Exception e) {
String errorMsg = String.format("Failed to log objects using GSON due to: [%s].", e.getMessage());
LOGGER.error(errorMsg, e);
LOGGER.trace(errorMsg, e);
return errorMsg;
}
}