NIFI-4277 Fixed exception logging in StandardLogRepository

This closes #2068.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
Pierre Villard 2017-08-09 11:50:53 +02:00 committed by Andy LoPresto
parent 528b82634f
commit 18c82eb6af
No known key found for this signature in database
GPG Key ID: 6EC293152D90B61D
2 changed files with 72 additions and 5 deletions

View File

@ -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();

View File

@ -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;
}
}
}