From 803868935afc424bf0b69b6b82802e557e689c04 Mon Sep 17 00:00:00 2001 From: Peter Turcsanyi Date: Thu, 1 Jun 2023 22:19:23 +0200 Subject: [PATCH] NIFI-11628 Fixed Object[] and Throwable argument substitution in SimpleProcessLogger This closes #7362 Signed-off-by: David Handermann --- .../nifi/processor/SimpleProcessLogger.java | 14 ++++++--- .../processor/TestSimpleProcessLogger.java | 31 ++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java index 32f19e328a..c7d8e8eab5 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java @@ -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); diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java index 2ed6723114..5e5d33fd13 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java @@ -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()) {