Skip to content

Commit 1b06184

Browse files
author
anders-wartoft
committed
1.1-3. Updates to (mostly) regex filter
1 parent 2d90d81 commit 1b06184

File tree

11 files changed

+89
-14
lines changed

11 files changed

+89
-14
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target
22

3-
.vscode
3+
.vscode
4+
.DS_Store

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ java -jar target/LogGenerator{version}.jar -i kafka -ci test3 -t OUTPUT -b 192.1
1818
When running the last command, press Ctrl-C to see the gaps in the received data. Since we started the counter on 100, there should at least be one gap: 1-99.
1919

2020
### Latest Release Notes
21+
#### 1.1-3
22+
Added a parameter {all} to regex filter, so a regex filter can wrap the log in a new string. Also, fixed a bug that hindered the regex filter to escape quotes (changing " to \").
23+
Minor documentation updates.
24+
2125
#### 1.1-2
2226
Update of documentation. E.g., -h is no longer valid as --hostname shorthand. Also, update of `-f guard`. In 1.1-1, the `-f guard` command removed all content in the event but not the event itself, so if the event was written to file, an empty line would be the result. In 1.1-2, the event is correctly removed.
2327

@@ -387,7 +391,7 @@ There must be a capture group in the regex. The text matched by the capture grou
387391

388392
Parameters: `-f regex --regex {regex to find} --value {value to insert instead of the part matched by the regex} [--time-offset {offset in milliseconds}]`
389393

390-
Example: `-f regex --regex "<(\d+)>" -value "<2>"`
394+
Example: `-f regex --regex "<(\d+)>" --value "<2>"`
391395

392396
If you have a file with a lot of logs, like:
393397

@@ -399,6 +403,29 @@ Then the following invocation will change the date to today:
399403

400404
You can also use the --time-offset to change the date to a date that is not the current date. For example, if you want to send events with a date that is one day ago, use `-f regex --regex "([a-zA-Z]{3} [a-zA-Z]{3} \d\d \d\d:\d\d:\d\d\.\d{3})" --value "{date:EEE MMM HH:mm:ss.sss}" --time-offset -86400000`
401405

406+
There is also a way to wrap the result. E.g., if you have a string
407+
`
408+
TestString
409+
`
410+
and want to wrap that in json, you can use
411+
```json
412+
java -jar LogGenerator-{version}.jar -i static --string TestString -f regex --regex "^.*$" --value "{\"parameter\": \"{all}\"}" -l 1 -o cmd
413+
```
414+
Result:
415+
```json
416+
{"parameter": "TestString"}
417+
```
418+
419+
in case the static string contains quotes, then use a regex filter to change those first:
420+
```json
421+
java -jar LogGenerator-{version}.jar -i static --string "{\"parameter\":\"TestString\"}" -f regex --regex "\"" --value "\\\\\"" -f regex --regex "^.*$" --value "{\"parameter\": \"{all}\"}" -l 1 -o cmd
422+
```
423+
resulting in:
424+
```json
425+
{"parameter": "{\"a\":\"TestString\"}"}
426+
```
427+
428+
402429
#### Replace variables
403430
Variable substitution will be present for template, regex and header processing. If a file is loaded as "file" or template "none" then the (processor intensive) substitutions will not be loaded.
404431

dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>nu.sitia.LogGenerator</groupId>
55
<artifactId>LogGenerator</artifactId>
66
<name>LogGenerator</name>
7-
<version>1.1-2</version>
7+
<version>1.1-3</version>
88
<url>http://maven.apache.org</url>
99
<build>
1010
<plugins>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>nu.sitia.LogGenerator</groupId>
66
<artifactId>LogGenerator</artifactId>
7-
<version>1.1-2</version>
7+
<version>1.1-3</version>
88
<packaging>jar</packaging>
99

1010
<name>LogGenerator</name>

src/main/java/nu/sitia/loggenerator/filter/RegexFilter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Date;
2626
import java.util.HashMap;
2727
import java.util.List;
28+
import java.util.Map;
2829
import java.util.logging.Logger;
2930
import java.util.regex.Matcher;
3031
import java.util.regex.Pattern;
@@ -119,8 +120,10 @@ private String filter(String toFilter) {
119120
// Create a new date that represents now. Then, add the
120121
// offset provided by the user (positive for future and negative for in the past
121122
final Date date = new Date(new Date().getTime() + this.timeOffset);
122-
String newValue = substitution.substitute(value, new HashMap<>(), date);
123-
return toFilter.replaceAll(matcher.group(), newValue);
123+
HashMap<String, String> replaceMap = new HashMap<>();
124+
replaceMap.put("all", toFilter);
125+
String newValue = substitution.substitute(value, replaceMap, date);
126+
return matcher.replaceAll(Matcher.quoteReplacement(newValue));
124127
}
125128
return toFilter;
126129
}

src/main/java/nu/sitia/loggenerator/inputitems/TCPInputItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public boolean setParameter(String key, String value) {
4949
if (key != null && (key.equalsIgnoreCase("--help") || key.equalsIgnoreCase("-h"))) {
5050
System.out.println("TCPInputItem. Read from a TCP socket\n" +
5151
"Parameters:\n" +
52-
"--hostname <hostname> (-h <hostname>)\n" +
52+
"--hostname <hostname> (-hn <hostname>)\n" +
5353
" The hostname to bind to. If not specified, bind to all interfaces\n" +
5454
"--port <port> (-p <port>)\n" +
5555
" The port to listen on\n");
@@ -58,7 +58,7 @@ public boolean setParameter(String key, String value) {
5858
if (super.setParameter(key, value)) {
5959
return true;
6060
}
61-
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-h"))) {
61+
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-hn"))) {
6262
this.hostName = value;
6363
logger.fine("hostname " + value);
6464
return true;

src/main/java/nu/sitia/loggenerator/inputitems/UDPInputItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean setParameter(String key, String value) {
5050
if (key != null && (key.equalsIgnoreCase("--help") || key.equalsIgnoreCase("-h"))) {
5151
System.out.println("UDPInputItem. Read from a UDP socket\n" +
5252
"Parameters:\n" +
53-
"--hostname <hostname> (-h <hostname>)\n" +
53+
"--hostname <hostname> (-hn <hostname>)\n" +
5454
" The hostname to bind to. If not specified, bind to all interfaces\n" +
5555
"--port <port> (-p <port>)\n" +
5656
" The port to listen on\n");
@@ -59,7 +59,7 @@ public boolean setParameter(String key, String value) {
5959
if (super.setParameter(key, value)) {
6060
return true;
6161
}
62-
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-h"))) {
62+
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-hn"))) {
6363
this.hostName = value;
6464
logger.fine("hostname " + value);
6565
return true;

src/main/java/nu/sitia/loggenerator/outputitems/TCPOutputItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public boolean setParameter(String key, String value) {
5252
if (key != null && (key.equalsIgnoreCase("--help") || key.equalsIgnoreCase("-h"))) {
5353
System.out.println("TCPOutputItem. Write to TCP socket\n" +
5454
"Parameters:\n" +
55-
"--hostname, -h <hostname> The hostname to connect to\n" +
55+
"--hostname, -hn <hostname> The hostname to connect to\n" +
5656
"--port, -p <port> The port to connect to\n");
5757
System.exit(1);
5858
}
5959
if (super.setParameter(key, value)) {
6060
return true;
6161
}
62-
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-h"))) {
62+
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-hn"))) {
6363
this.hostName = value;
6464
logger.fine("hostname " + value);
6565
return true;

src/main/java/nu/sitia/loggenerator/outputitems/UDPOutputItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public boolean setParameter(String key, String value) {
5656
if (key != null && (key.equalsIgnoreCase("--help") || key.equalsIgnoreCase("-h"))) {
5757
System.out.println("UDPOutputItem. Write to UDP socket\n" +
5858
"Parameters:\n" +
59-
"--hostname, -h <hostname> The hostname to connect to\n" +
59+
"--hostname, -hn <hostname> The hostname to connect to\n" +
6060
"--port, -p <port> The port to connect to\n");
6161
System.exit(1);
6262
}
6363
if (super.setParameter(key, value)) {
6464
return true;
6565
}
66-
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-h"))) {
66+
if (key != null && (key.equalsIgnoreCase("--hostname") || key.equalsIgnoreCase("-hn"))) {
6767
this.hostName = value;
6868
logger.fine("hostname " + value);
6969
return true;

0 commit comments

Comments
 (0)