Skip to content
Snippets Groups Projects
Commit ca621a00 authored by felmer's avatar felmer
Browse files

BufferedAppender: Filter also the content. This should increase robustness of...

BufferedAppender: Filter also the content. This should increase robustness of the tests which checks logging events
parent 43f59d7e
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ package ch.systemsx.cisd.common.logging; ...@@ -18,6 +18,7 @@ package ch.systemsx.cisd.common.logging;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -43,6 +44,8 @@ public final class BufferedAppender extends WriterAppender ...@@ -43,6 +44,8 @@ public final class BufferedAppender extends WriterAppender
{ {
private final ByteArrayOutputStream logRecorder; private final ByteArrayOutputStream logRecorder;
private final List<Pattern> patternOfSuppressedLogEvents = new LinkedList<Pattern>();
/** /**
* Constructor with default pattern layout (which is {@link PatternLayout#DEFAULT_CONVERSION_PATTERN}) and {@link Level#DEBUG} as log level. * Constructor with default pattern layout (which is {@link PatternLayout#DEFAULT_CONVERSION_PATTERN}) and {@link Level#DEBUG} as log level.
*/ */
...@@ -103,6 +106,7 @@ public final class BufferedAppender extends WriterAppender ...@@ -103,6 +106,7 @@ public final class BufferedAppender extends WriterAppender
return pattern.matcher(loggerName).matches() ? Filter.DENY : Filter.ACCEPT; return pattern.matcher(loggerName).matches() ? Filter.DENY : Filter.ACCEPT;
} }
}); });
patternOfSuppressedLogEvents.add(Pattern.compile(".*" + loggerNameRegex + ".*"));
} }
...@@ -124,7 +128,29 @@ public final class BufferedAppender extends WriterAppender ...@@ -124,7 +128,29 @@ public final class BufferedAppender extends WriterAppender
*/ */
public final String getLogContent() public final String getLogContent()
{ {
return new String(logRecorder.toByteArray()).trim(); String content = new String(logRecorder.toByteArray()).trim();
String[] split = content.split("\n");
StringBuilder builder = new StringBuilder();
for (String line : split)
{
if (shouldBeSuppressed(line) == false)
{
builder.append(line).append("\n");
}
}
return builder.toString().trim();
}
private boolean shouldBeSuppressed(String line)
{
for (Pattern pattern : patternOfSuppressedLogEvents)
{
if (pattern.matcher(line).matches())
{
return true;
}
}
return false;
} }
public List<String> getLogLines() public List<String> getLogLines()
......
/*
* Copyright 2018 ETH Zuerich, SIS
*
* Licensed 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 ch.systemsx.cisd.common.logging;
import static org.testng.Assert.assertEquals;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
import org.testng.annotations.Test;
/**
* @author Franz-Josef Elmer
*
*/
public class BufferedAppenderTest
{
@Test
public void test()
{
// Given
BufferedAppender appender = new BufferedAppender("%-5p %c - %m%n", Level.DEBUG);
appender.addRegexForLoggingEventsToBeDropped("ab.*f");
System.err.println(appender.getFilter());
// Then
appender.append(new LoggingEvent("my-class", LogManager.getRootLogger(), 123456, Level.INFO,
"testing", "my-thread", null, "ndc", new LocationInfo(null, null), null));
appender.append(new LoggingEvent("my-class", LogManager.getRootLogger(), 123456, Level.INFO,
"abcdef", "my-thread", null, "ndc", new LocationInfo(null, null), null));
// When
assertEquals(appender.getLogContent(), "INFO root - testing");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment