You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by al...@apache.org on 2017/08/10 18:55:33 UTC

nifi git commit: NIFI-4277 Fixed exception logging in StandardLogRepository

Repository: nifi
Updated Branches:
  refs/heads/master 528b82634 -> 18c82eb6a


NIFI-4277 Fixed exception logging in StandardLogRepository

This closes #2068.

Signed-off-by: Andy LoPresto <al...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/18c82eb6
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/18c82eb6
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/18c82eb6

Branch: refs/heads/master
Commit: 18c82eb6af6648460365f8db286729629258e95c
Parents: 528b826
Author: Pierre Villard <pi...@gmail.com>
Authored: Wed Aug 9 11:50:53 2017 +0200
Committer: Andy LoPresto <al...@apache.org>
Committed: Thu Aug 10 11:54:32 2017 -0700

----------------------------------------------------------------------
 .../repository/StandardLogRepository.java       | 16 +++--
 .../nifi/logging/TestStandardLogRepository.java | 61 ++++++++++++++++++++
 2 files changed, 72 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/18c82eb6/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/logging/repository/StandardLogRepository.java
----------------------------------------------------------------------
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 2d10d82..c610f8c 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();

http://git-wip-us.apache.org/repos/asf/nifi/blob/18c82eb6/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/logging/TestStandardLogRepository.java
----------------------------------------------------------------------
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 0000000..6017220
--- /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 <exception>");
+        assertEquals(observer.getMessages().get(1), "Testing org.apache.nifi.logging.TestStandardLogRepository$MockLogObserver to get exception message");
+    }
+
+    private class MockLogObserver implements LogObserver {
+
+        private List<String> messages = new ArrayList<String>();
+
+        @Override
+        public void onLogMessage(LogMessage message) {
+            messages.add(message.getMessage());
+        }
+
+        public List<String> getMessages() {
+            return messages;
+        }
+
+    }
+
+}
\ No newline at end of file