All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.github.davidebocca.util.unit.test.utils.LoggingUtils Maven / Gradle / Ivy
/**
*
*/
package io.github.davidebocca.util.unit.test.utils;
import java.text.MessageFormat;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import io.github.davidebocca.util.unit.test.exception.ErrorCodeEnum;
import io.github.davidebocca.util.unit.test.rules.utils.RuleIdEnum;
/**
* @author cr10248
*
*/
public class LoggingUtils {
private LoggingUtils() {
// do nothing
}
private static final Logger LOGGER = LogManager.getLogger(LoggingUtils.class);
public static void logTestStep(RuleIdEnum ruleIdEnum, String message) {
LOGGER.debug(generateTestStepMessage(ruleIdEnum, message));
}
public static void logTestWarning(RuleIdEnum ruleIdEnum, String message, String... extraParams) {
LOGGER.warn(generateGenericMessage(ruleIdEnum, message, extraParams));
}
public static void manageError(ErrorCodeEnum errorCode, String... extraParams) {
String message = generateErrorMessage(errorCode, null, extraParams);
LOGGER.error(message);
}
public static void manageErrorWithException(ErrorCodeEnum errorCode, Throwable exception, String... extraParams) {
String message = generateErrorMessage(errorCode, exception, extraParams);
if (exception == null) {
LOGGER.error(message);
} else {
LOGGER.error(message, exception);
}
}
private static String generateGenericMessage(RuleIdEnum ruleIdEnum, String message, String... extraParams) {
String msg = MessageFormat.format("Rule {0}, Message: {1}", ruleIdEnum.name(), message);
return msg.concat(getExtraParamsString(extraParams));
}
private static String generateTestStepMessage(RuleIdEnum ruleIdEnum, String message) {
return MessageFormat.format("Test Step - Rule {0}, Message: {1}", ruleIdEnum.name(), message);
}
private static String generateErrorMessage(ErrorCodeEnum errorCode, Throwable exception, String... extraParams) {
String message;
if (errorCode.getTest() == null) {
message = MessageFormat.format("Error code: {0} - {1}",
errorCode.getErrorCode(), errorCode.getErrorMessage());
} else {
message = MessageFormat.format("Error code: {0} - Test: {1} - {2}",
errorCode.getErrorCode(), errorCode.getTest().name(), errorCode.getErrorMessage());
}
message = message.concat(getExtraParamsString(extraParams));
if (exception != null) {
message = message.concat(ExceptionUtils.getStackTrace(exception));
}
return message;
}
public static String getExtraParamsString(String... extraParams) {
String tmp = StringUtils.EMPTY;
for (int i = 1; i - 1 < extraParams.length; i++) {
String p = extraParams[i - 1];
if (StringUtils.isNotBlank(p)) {
tmp = tmp.concat(MessageFormat.format(" - {0}", p));
}
}
return tmp;
}
}