[
https://issues.apache.org/jira/browse/KARAF-5801?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16606133#comment-16606133
]
ASF GitHub Bot commented on KARAF-5801:
---------------------------------------
fpapon closed pull request #48: [KARAF-5801] Add regex filtering in the file
collector and fix tailer threading
URL: https://github.com/apache/karaf-decanter/pull/48
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
b/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
index 098ae6d..0894b81 100644
---
a/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
+++
b/collector/file/src/main/java/org/apache/karaf/decanter/collector/file/DecanterTailerListener.java
@@ -20,8 +20,8 @@
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;
@@ -52,12 +52,13 @@
private String type;
private String path;
+ private String regex;
/**
* additional properties provided by the user
*/
private Dictionary<String, Object> properties;
- private ExecutorService executorService;
+ private Tailer tailer;
@SuppressWarnings("unchecked")
@Activate
@@ -73,16 +74,17 @@ public void activate(ComponentContext context) throws
ConfigurationException {
String path = (String) properties.get("path");
LOGGER.debug("Starting tail on {}", path);
- Tailer tailer = Tailer.create(new File(path), this, 1000, true, true);
- executorService = Executors.newSingleThreadExecutor();
- executorService.submit(tailer);
+ tailer = new Tailer(new File(path), this, 1000, true, true);
+ Thread thread = new Thread(tailer, "File tailer for " + path);
this.type = type;
this.path = path;
+ this.regex = (String) properties.get("regex");
+ thread.start();
}
@Deactivate
public void deactivate() {
- executorService.shutdownNow();
+ tailer.stop();
}
@Override
@@ -91,9 +93,20 @@ public void handle(String line) {
Map<String, Object> data = new HashMap<>();
data.put("type", type);
data.put("path", path);
+ data.put("regex", regex);
// TODO: try some line parsing
- data.put("line", line);
+ if (regex != null) {
+ Pattern pattern = Pattern.compile(regex);
+ Matcher matcher = pattern.matcher(line);
+ if (matcher.matches()) {
+ data.put("line", line);
+ } else {
+ return;
+ }
+ } else {
+ data.put("line", line);
+ }
try {
PropertiesPreparator.prepare(data, properties);
diff --git a/manual/src/main/asciidoc/user-guide/collectors.adoc
b/manual/src/main/asciidoc/user-guide/collectors.adoc
index 94340c8..3b03541 100644
--- a/manual/src/main/asciidoc/user-guide/collectors.adoc
+++ b/manual/src/main/asciidoc/user-guide/collectors.adoc
@@ -127,16 +127,27 @@ any=value
* all other values (like `any`) will be part of the collected data. It means
that you can add your own custom data, and
easily create queries bases on this data.
+You can also filter the lines read from the file using the `regex` property.
+
+For instance:
+
+----
+regex=(.*foo.*)
+----
+
+Only the line matching the `regex` will be sent to the dispatcher.
+
For instance, instead of the log collector, you can create the following
`etc/org.apache.karaf.decanter.collector.file-karaf.cfg`
file collector configuration file:
----
type=karaf-log-file
path=/path/to/karaf/data/log/karaf.log
+regex=(.*my.*)
my=stuff
----
-The file collector will tail on karaf.log file, and send any new line in this
log file as collected data.
+The file collector will tail on `karaf.log` file, and send any new line
matching the `regex` in this log file as collected data.
==== EventAdmin
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Add regex filtering on collector file
> -------------------------------------
>
> Key: KARAF-5801
> URL: https://issues.apache.org/jira/browse/KARAF-5801
> Project: Karaf
> Issue Type: Improvement
> Components: decanter
> Reporter: Jean-Baptiste Onofré
> Assignee: Jean-Baptiste Onofré
> Priority: Major
> Fix For: decanter-2.1.0
>
>
> Now the decanter file collector collects the new line in the file (tailer)
> and send each line to the dispatcher. It would be great to be able to filter
> some lines depending of a regex.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)