diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/logging/repository/StandardLogRepository.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/logging/repository/StandardLogRepository.java index 2d10d820ac..c610f8caee 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/logging/repository/StandardLogRepository.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/logging/repository/StandardLogRepository.java @@ -70,20 +70,26 @@ public class StandardLogRepository implements LogRepository { @Override public void addLogMessage(final LogLevel level, final String format, final Object[] params) { + replaceThrowablesWithMessage(params); final String formattedMessage = MessageFormatter.arrayFormat(format, params).getMessage(); addLogMessage(level, formattedMessage); } @Override public void addLogMessage(final LogLevel level, final String format, final Object[] params, final Throwable t) { - final Object[] paramsWithThrowable = new Object[params.length + 1]; - System.arraycopy(params, 0, paramsWithThrowable, 0, params.length); - paramsWithThrowable[paramsWithThrowable.length - 1] = t; - - final String formattedMessage = MessageFormatter.arrayFormat(format, paramsWithThrowable).getMessage(); + replaceThrowablesWithMessage(params); + final String formattedMessage = MessageFormatter.arrayFormat(format, params, t).getMessage(); addLogMessage(level, formattedMessage, t); } + private void replaceThrowablesWithMessage(Object[] params) { + for (int i = 0; i < params.length; i++) { + if(params[i] instanceof Throwable) { + params[i] = ((Throwable) params[i]).getLocalizedMessage(); + } + } + } + @Override public void setObservationLevel(String observerIdentifier, LogLevel level) { writeLock.lock(); diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/logging/TestStandardLogRepository.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/logging/TestStandardLogRepository.java new file mode 100644 index 0000000000..6017220cfb --- /dev/null +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/logging/TestStandardLogRepository.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.logging; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.nifi.logging.repository.StandardLogRepository; +import org.junit.Test; + +public class TestStandardLogRepository { + + @Test + public void testLogRepository() { + StandardLogRepository repo = new StandardLogRepository(); + MockLogObserver observer = new MockLogObserver(); + repo.addObserver("mock", LogLevel.DEBUG, observer); + + IOException exception = new IOException("exception"); + + repo.addLogMessage(LogLevel.DEBUG, "Testing {} to get exception message <{}>", new Object[]{observer.getClass().getName(), exception}); + repo.addLogMessage(LogLevel.DEBUG, "Testing {} to get exception message", new Object[]{observer.getClass().getName()}, exception); + + assertEquals(observer.getMessages().get(0), "Testing org.apache.nifi.logging.TestStandardLogRepository$MockLogObserver to get exception message "); + assertEquals(observer.getMessages().get(1), "Testing org.apache.nifi.logging.TestStandardLogRepository$MockLogObserver to get exception message"); + } + + private class MockLogObserver implements LogObserver { + + private List messages = new ArrayList(); + + @Override + public void onLogMessage(LogMessage message) { + messages.add(message.getMessage()); + } + + public List getMessages() { + return messages; + } + + } + +} \ No newline at end of file