NIFI-11628 Fixed Object[] and Throwable argument substitution in SimpleProcessLogger

This closes #7362

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Peter Turcsanyi 2023-06-01 22:19:23 +02:00 committed by exceptionfactory
parent df9f8ddcfb
commit 803868935a
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
2 changed files with 39 additions and 6 deletions

View File

@ -91,7 +91,7 @@ public class SimpleProcessLogger implements ComponentLog {
logger.warn(componentMessage, arguments);
logRepository.addLogMessage(LogLevel.WARN, componentMessage, arguments);
} else {
logger.warn(componentMessage, arguments, t);
logger.warn(componentMessage, addThrowable(arguments, t));
logRepository.addLogMessage(LogLevel.WARN, getCausesMessage(msg), addCauses(arguments, t), t);
}
}
@ -158,7 +158,7 @@ public class SimpleProcessLogger implements ComponentLog {
logger.trace(componentMessage, arguments);
logRepository.addLogMessage(LogLevel.TRACE, componentMessage, arguments);
} else {
logger.trace(componentMessage, arguments, t);
logger.trace(componentMessage, addThrowable(arguments, t));
logRepository.addLogMessage(LogLevel.TRACE, getCausesMessage(msg), addCauses(arguments, t), t);
}
}
@ -245,7 +245,7 @@ public class SimpleProcessLogger implements ComponentLog {
logger.info(componentMessage, arguments);
logRepository.addLogMessage(LogLevel.INFO, componentMessage, arguments);
} else {
logger.info(componentMessage, arguments, t);
logger.info(componentMessage, addThrowable(arguments, t));
logRepository.addLogMessage(LogLevel.INFO, getCausesMessage(msg), addCauses(arguments, t), t);
}
}
@ -312,7 +312,7 @@ public class SimpleProcessLogger implements ComponentLog {
logger.error(componentMessage, arguments);
logRepository.addLogMessage(LogLevel.ERROR, componentMessage, arguments);
} else {
logger.error(componentMessage, arguments, t);
logger.error(componentMessage, addThrowable(arguments, t));
logRepository.addLogMessage(LogLevel.ERROR, getCausesMessage(msg), addCauses(arguments, t), t);
}
}
@ -369,7 +369,7 @@ public class SimpleProcessLogger implements ComponentLog {
logger.debug(componentMessage, arguments);
logRepository.addLogMessage(LogLevel.DEBUG, componentMessage, arguments);
} else {
logger.debug(componentMessage, arguments, t);
logger.debug(componentMessage, addThrowable(arguments, t));
logRepository.addLogMessage(LogLevel.DEBUG, getCausesMessage(msg), addCauses(arguments, t), t);
}
}
@ -533,6 +533,10 @@ public class SimpleProcessLogger implements ComponentLog {
return ArrayUtils.insert(0, originalArgs, component);
}
private Object[] addThrowable(final Object[] originalArgs, final Throwable throwable) {
return ArrayUtils.add(originalArgs, throwable);
}
private Object[] addCauses(final Object[] arguments, final Throwable throwable) {
final String causes = getCauses(throwable);
return ArrayUtils.add(arguments, causes);

View File

@ -200,7 +200,7 @@ public class TestSimpleProcessLogger {
switch (logLevel) {
case TRACE:
verify(logger).trace(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION_STRING), eq(EXCEPTION));
verify(logger).trace(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION_STRING), eq(EXCEPTION));
break;
case DEBUG:
verify(logger).debug(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION_STRING), eq(EXCEPTION));
@ -222,6 +222,35 @@ public class TestSimpleProcessLogger {
}
}
@Test
public void testLogLevelMessageArgumentsArrayThrowable() {
for (final LogLevel logLevel : LogLevel.values()) {
componentLog.log(logLevel, LOG_ARGUMENTS_MESSAGE, VALUE_ARGUMENTS, EXCEPTION);
switch (logLevel) {
case TRACE:
verify(logger).trace(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION));
break;
case DEBUG:
verify(logger).debug(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION));
break;
case INFO:
verify(logger).info(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION));
break;
case WARN:
verify(logger).warn(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION));
break;
case ERROR:
verify(logger).error(eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT), eq(component), eq(FIRST), eq(SECOND), eq(EXCEPTION));
break;
default:
continue;
}
verify(logRepository).addLogMessage(eq(logLevel), eq(LOG_ARGUMENTS_MESSAGE_WITH_COMPONENT_AND_CAUSES), eq(componentValueCausesArguments), eq(EXCEPTION));
}
}
@Test
public void testLogLevelMessageArgumentsThrowableNull() {
for (final LogLevel logLevel : LogLevel.values()) {