Skip to content

Commit

Permalink
error as info (#67)
Browse files Browse the repository at this point in the history
* improve logging utils

* formatting
  • Loading branch information
azahnen authored Aug 13, 2021
1 parent b22c86f commit 183cf12
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.event.Level;

/** @author zahnen */
public class LogContext {
Expand Down Expand Up @@ -129,7 +130,24 @@ public static void error(
Object[] args = Arrays.copyOf(messagePrefixArgs, messagePrefixArgs.length + 1);
args[messagePrefixArgs.length] = getMessage(throwable);

logThrowable(logger, throwable, messagePrefix + ": {}", args);
logThrowable(logger, Level.ERROR, throwable, messagePrefix + ": {}", args);
}

/**
* Log the message of a Throwable with a custom prefix in the INFO level and log the stacktrace of
* the Throwable in the DEBUG level with the STACKTRACE marker.
*
* @param logger The Logger to use
* @param throwable The throwable to log
* @param messagePrefix The message prefix
* @param messagePrefixArgs The message prefix substitutions
*/
public static void errorAsInfo(
Logger logger, Throwable throwable, String messagePrefix, Object... messagePrefixArgs) {
Object[] args = Arrays.copyOf(messagePrefixArgs, messagePrefixArgs.length + 1);
args[messagePrefixArgs.length] = getMessage(throwable);

logThrowable(logger, Level.INFO, throwable, messagePrefix + ": {}", args);
}

/**
Expand Down Expand Up @@ -161,12 +179,16 @@ public static void errorChain(
System.arraycopy(messages, 0, args, messagePrefixArgs.length, numMessages);
}

logThrowable(logger, throwable, prefix, args);
logThrowable(logger, Level.ERROR, throwable, prefix, args);
}

private static void logThrowable(
Logger logger, Throwable throwable, String message, Object... messageArgs) {
logger.error(message, messageArgs);
Logger logger, Level level, Throwable throwable, String message, Object... messageArgs) {
if (level == Level.INFO) {
logger.info(message, messageArgs);
} else {
logger.error(message, messageArgs);
}
if (logger.isDebugEnabled(MARKER.STACKTRACE)) {
logger.debug(MARKER.STACKTRACE, "Stacktrace:", throwable);
}
Expand Down

0 comments on commit 183cf12

Please sign in to comment.