aboutsummaryrefslogtreecommitdiffstats
path: root/utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java
diff options
context:
space:
mode:
Diffstat (limited to 'utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java')
-rw-r--r--utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java289
1 files changed, 144 insertions, 145 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java b/utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java
index 14bceb4c..5ccb13ee 100644
--- a/utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java
+++ b/utils-test/src/main/java/org/onap/policy/common/utils/test/log/logback/ExtractAppender.java
@@ -20,6 +20,8 @@
package org.onap.policy.common.utils.test.log.logback;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.AppenderBase;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -28,9 +30,6 @@ import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import ch.qos.logback.classic.spi.ILoggingEvent;
-import ch.qos.logback.core.AppenderBase;
-
/**
* This is an appender that is intended for use by JUnit tests that wish to
* capture logged messages. The appender takes an optional list of regular
@@ -46,147 +45,147 @@ import ch.qos.logback.core.AppenderBase;
*/
public class ExtractAppender extends AppenderBase<ILoggingEvent> {
- /**
- * Extracted text is placed here.
- */
- private final Queue<String> extracted;
-
- /**
- * Regular expressions/Patterns to be used to extract text. Uses a
- * LinkedHashMap so that order is preserved.
- */
- private final LinkedHashMap<String, Pattern> patterns;
-
- /**
- * Records every message that is logged.
- */
- public ExtractAppender() {
- this(new LinkedList<>());
- }
-
- /**
- * Records portions of messages that match one of the regular
- * expressions.
- *
- * @param regex
- * regular expression (i.e., {@link Pattern}) to match
- */
- public ExtractAppender(final String... regex) {
- this(new LinkedList<>(), regex);
- }
-
- /**
- * Rather than allocating an internal queue to store matched messages,
- * messages are recorded in the specified target queue using the
- * {@link Queue#offer(Object)} method. Note: whenever the queue is used,
- * it will be synchronized to prevent simultaneous accesses.
- *
- * @param target - queue into which the matched text should be placed
- * @param regex regular expression (i.e., {@link Pattern}) to match
- */
- public ExtractAppender(final Queue<String> target,
- final String... regex) {
- extracted = target;
- patterns = new LinkedHashMap<>(regex.length);
-
- for (String re : regex) {
- patterns.put(re, Pattern.compile(re));
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see ch.qos.logback.core.AppenderBase#append(Object)
- */
- @Override
- protected void append(final ILoggingEvent event) {
-
- String msg = event.getMessage();
-
- synchronized (patterns) {
- if (patterns.isEmpty()) {
- addExtraction(msg);
- return;
- }
-
- for (Pattern p : patterns.values()) {
- Matcher m = p.matcher(msg);
-
- if (m.find()) {
- addGroupMatch(m);
- break;
- }
- }
- }
- }
-
- /**
- * Adds the first match group to {@link #extracted}.
- *
- * @param mat the matcher containing the groups
- *
- */
- private void addGroupMatch(final Matcher mat) {
- int ngroups = mat.groupCount();
-
- for (int x = 1; x <= ngroups; ++x) {
- String txt = mat.group(x);
-
- if (txt != null) {
- addExtraction(txt);
- return;
- }
- }
-
- addExtraction(mat.group());
- }
-
- /**
- * Adds an item to {@link #extracted}, in a thread-safe manner.
- * It uses the queue's <i>offer()</i> method so that the queue
- * can discard the item if it so chooses, without generating
- * an exception.
- *
- * @param txt
- * text to be added
- */
- private void addExtraction(final String txt) {
- synchronized (extracted) {
- extracted.offer(txt);
- }
- }
-
- /**
- * Gets the text that has been extracted.
- *
- * @return a copy of the text that has been extracted
- */
- public List<String> getExtracted() {
- synchronized (extracted) {
- return new ArrayList<>(extracted);
- }
- }
-
- /**
- * Clears the list of extracted text.
- */
- public void clearExtractions() {
- synchronized (extracted) {
- extracted.clear();
- }
- }
-
- /**
- * Adds a pattern to be matched by this appender.
- *
- * @param regex
- * regular expression (i.e., {@link Pattern}) to match
- */
- public void setPattern(final String regex) {
- synchronized (patterns) {
- patterns.put(regex, Pattern.compile(regex));
- }
- }
+ /**
+ * Extracted text is placed here.
+ */
+ private final Queue<String> extracted;
+
+ /**
+ * Regular expressions/Patterns to be used to extract text. Uses a
+ * LinkedHashMap so that order is preserved.
+ */
+ private final LinkedHashMap<String, Pattern> patterns;
+
+ /**
+ * Records every message that is logged.
+ */
+ public ExtractAppender() {
+ this(new LinkedList<>());
+ }
+
+ /**
+ * Records portions of messages that match one of the regular
+ * expressions.
+ *
+ * @param regex
+ * regular expression (i.e., {@link Pattern}) to match
+ */
+ public ExtractAppender(final String... regex) {
+ this(new LinkedList<>(), regex);
+ }
+
+ /**
+ * Rather than allocating an internal queue to store matched messages,
+ * messages are recorded in the specified target queue using the
+ * {@link Queue#offer(Object)} method. Note: whenever the queue is used,
+ * it will be synchronized to prevent simultaneous accesses.
+ *
+ * @param target - queue into which the matched text should be placed
+ * @param regex regular expression (i.e., {@link Pattern}) to match
+ */
+ public ExtractAppender(final Queue<String> target,
+ final String... regex) {
+ extracted = target;
+ patterns = new LinkedHashMap<>(regex.length);
+
+ for (String re : regex) {
+ patterns.put(re, Pattern.compile(re));
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see ch.qos.logback.core.AppenderBase#append(Object)
+ */
+ @Override
+ protected void append(final ILoggingEvent event) {
+
+ String msg = event.getMessage();
+
+ synchronized (patterns) {
+ if (patterns.isEmpty()) {
+ addExtraction(msg);
+ return;
+ }
+
+ for (Pattern p : patterns.values()) {
+ Matcher matcher = p.matcher(msg);
+
+ if (matcher.find()) {
+ addGroupMatch(matcher);
+ break;
+ }
+ }
+ }
+ }
+
+ /**
+ * Adds the first match group to {@link #extracted}.
+ *
+ * @param mat the matcher containing the groups
+ *
+ */
+ private void addGroupMatch(final Matcher mat) {
+ int ngroups = mat.groupCount();
+
+ for (int x = 1; x <= ngroups; ++x) {
+ String txt = mat.group(x);
+
+ if (txt != null) {
+ addExtraction(txt);
+ return;
+ }
+ }
+
+ addExtraction(mat.group());
+ }
+
+ /**
+ * Adds an item to {@link #extracted}, in a thread-safe manner.
+ * It uses the queue's <i>offer()</i> method so that the queue
+ * can discard the item if it so chooses, without generating
+ * an exception.
+ *
+ * @param txt
+ * text to be added
+ */
+ private void addExtraction(final String txt) {
+ synchronized (extracted) {
+ extracted.offer(txt);
+ }
+ }
+
+ /**
+ * Gets the text that has been extracted.
+ *
+ * @return a copy of the text that has been extracted
+ */
+ public List<String> getExtracted() {
+ synchronized (extracted) {
+ return new ArrayList<>(extracted);
+ }
+ }
+
+ /**
+ * Clears the list of extracted text.
+ */
+ public void clearExtractions() {
+ synchronized (extracted) {
+ extracted.clear();
+ }
+ }
+
+ /**
+ * Adds a pattern to be matched by this appender.
+ *
+ * @param regex
+ * regular expression (i.e., {@link Pattern}) to match
+ */
+ public void setPattern(final String regex) {
+ synchronized (patterns) {
+ patterns.put(regex, Pattern.compile(regex));
+ }
+ }
}