Skip to content

Commit

Permalink
Provide a main method for executing log-parser as a commandline tool (#…
Browse files Browse the repository at this point in the history
…155)

* Added unit tests for issue #10
* Finished integration of commandline execution of the log-parser
* Included the main tests, and corrected issue with misplaced tests
  • Loading branch information
baubakg authored Aug 6, 2024
1 parent 8282b8a commit 688372b
Show file tree
Hide file tree
Showing 11 changed files with 729 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ All code pushed onto the repository must pass te following quality gates:
* Passed Unit Tests
* Code Coverage may not go down
* The sonar quality gate should remain green
* All new files need to ontain the license header. This can be acheived by running `mvn license:format`.
* All new files need to contain the license header. This can be acheived by running `mvn license:format`.

These validations are done automatically through github actions.

Expand Down
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ The following dependency needs to be added to your pom file:
<version>1.0.10</version>
</dependency>
```
## Running the Log Parser
We have two ways of running the log parser:
1. Programmatically, as a library and in your test you can simply use the log-parser to analyse your log files.
2. Command-Line, as of version 1.11.0, we allow you to run your log-parsing from the command-line. Further details can be found in the section [Command-line Execution of the Log-Parser](#command-line-execution-of-the-log-parser).

## Parse Definitions
In order to parse logs you need to define a ParseDefinition. A ParseDefinition contains a set of ordered ParseDefinition Entries. While parsing a line of logs, the LogParser will see if all entries can be found in the line of logs. If that is the case, the line is stored according to the definitions.
Expand Down Expand Up @@ -165,8 +169,7 @@ In the code above we want to parse the log line below, and want to fin the REST
The code starts with the creation a parse definition with at least two parse definitions that tell us between which markers should each data be extracted. The parse difinition is then handed to the StringParseFactory so that the data can be extracted.
At the end we can see that each data is stored in a map with the parse defnition entry title as a key.


### Import and Export
### Import and Export of Parse Definitions
You can import or store a Parse Definition to or from a JSON file.

### Importing a JSON File
Expand Down Expand Up @@ -356,9 +359,39 @@ AssertLogData.assertLogContains(List<String> in_filePathList, ParseDefinition in
## Exporting Results to a CSV File
We now have the possibility to export the log data results into a CSV file. The file will be a concatenation of the Parse Definition file, suffixed with "-export.csv".

## Release Notes
## Exporting Results to a CSV File
We now have the possibility to export the log data results into a CSV file. The file will be a concatenation of the Parse Definition file, suffixed with "-export.csv".

## Command-line Execution of the Log-Parser
As of version 1.11.0 we have introduced the possibility of running the log-parser from the command line. This is done by using the executable jar file or executing the main method in maven.

The results will currently be stored as a CSV or HTML file.

The command line requires you to at least provide the following information:
* `--startDir` : The root path from which the logs should be searched.
* `--parseDefinition` : The path to the parse definition file.

The typical command line would look like this:
```
mvn exec:java -Dexec.args="--startDir=src/test/resources/nestedDirs/ --parseDefinition=src/test/resources/parseDefinition.json"
```
or
```
java -jar log-parser-1.11.0.jar --startDir=/path/to/logs --parseDefinition=/path/to/parseDefinition.json
```

You can provide additional information such as:
* `--fileFilter` : The wildcard used for selecting the log files. The default value is *.log
* `--reportType` : The format of the report. The allowed values are currently HTML & CSV. The default value is HTML
* `--reportFileName` : The name of the report file. By default, this is the name of the Parse Definition name suffixed with '-export'
* `--reportName` : The report title as show in an HTML report. By default the title includes the Parse Definition name

You can get a print out of the command line options by running the command with the `--help` flag.

All reports are stored in the directory `./log-parser-reports/export/`.

### 1.11.0 (next version)
- **(new feature)** [#10](https://github.com/adobe/log-parser/issues/10) We now have an executable for the log-parser. You can perform a log parsing using the command line. For more information please read the section on [Command-line Execution of the Log-Parser](#command-line-execution-of-the-log-parser).
- **(new feature)** [#127](https://github.com/adobe/log-parser/issues/127) You can now compare two LogData Objects. This is a light compare that checks that for a given key, if it is absent, added or changes in frequency.
- **(new feature)** [#137](https://github.com/adobe/log-parser/issues/137) We can now generate an HTML report for the differences in log data.
- **(new feature)** [#138](https://github.com/adobe/log-parser/issues/138) We now have the possibility of anonymizing log data during parsing. For more information please read the section on [Anonymizing Data](#anonymizing-data).
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
<goals>deploy</goals>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<mainClass>com.adobe.campaign.tests.logparser.RunLogParser</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/com/adobe/campaign/tests/logparser/RunLogParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2022 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it.
*/
package com.adobe.campaign.tests.logparser;

import com.adobe.campaign.tests.logparser.core.LogData;
import com.adobe.campaign.tests.logparser.core.LogDataFactory;
import com.adobe.campaign.tests.logparser.core.ParseDefinition;
import com.adobe.campaign.tests.logparser.core.ParseDefinitionFactory;
import com.adobe.campaign.tests.logparser.exceptions.StringParseException;
import com.adobe.campaign.tests.logparser.utils.RunArguments;

import java.io.File;
import java.util.Arrays;

public class RunLogParser {
/**
* @param in_args
* @throws Exception
*/
public static void main(String[] in_args) throws StringParseException {
//Print help if requested
if (Arrays.stream(in_args).anyMatch(arg -> RunArguments.HELP.correspondsTo(arg))) {
RunArguments.printHelp();
return;
}

StringBuilder l_mandatories = new StringBuilder();
//Check if the mandatory values are there
RunArguments.getMandatoryCommands().stream().forEach(mc -> {
if (mc.fetchValue(in_args, null) == null) {
l_mandatories.append("Mandatory argument ").append(mc.buildTag()).append(" is missing").append("\n");
}
});

if (l_mandatories.length() > 0) {
System.err.println(l_mandatories.toString());
RunArguments.printHelp();
return;
}

//Fetch parse definition
ParseDefinition l_parseDefinition = ParseDefinitionFactory.importParseDefinition(
RunArguments.PARSE_DEFINITIONS_FILE.fetchValue(in_args));

//Extract the target SDK class
Class l_targetSDKClass;
try {
l_targetSDKClass = Class.forName(RunArguments.TARGET_SDK_CLASS.fetchValue(in_args));
} catch (ClassNotFoundException e) {
System.err.println("The target SDK class " + RunArguments.TARGET_SDK_CLASS.fetchValue(in_args)
+ " could not be found");
return;
}

//Generate Log data
LogData l_logData = LogDataFactory.generateLogData(RunArguments.START_DIR.fetchValue(in_args),
RunArguments.FILTER_LOG_FILES.fetchValue(in_args), l_parseDefinition,
l_targetSDKClass);

//Generate Report
if (RunArguments.REPORT_FORMAT.fetchValue(in_args).equalsIgnoreCase("CSV")) {
File x = l_logData.exportLogDataToCSV(RunArguments.REPORT_FILENAME.fetchValue(in_args,
l_parseDefinition.fetchEscapedTitle() + "-export.csv"));
} else if (RunArguments.REPORT_FORMAT.fetchValue(in_args).equalsIgnoreCase("HTML")) {
File x = l_logData.exportLogDataToHTML(
RunArguments.REPORT_NAME.fetchValue(in_args, l_parseDefinition.getTitle()),
RunArguments.REPORT_FILENAME.fetchValue(in_args,
l_parseDefinition.fetchEscapedTitle() + "-export"));
} else {
System.err.println("The report format " + RunArguments.REPORT_FORMAT.fetchValue(in_args)
+ " is not supported.");
}

}
}
20 changes: 18 additions & 2 deletions src/main/java/com/adobe/campaign/tests/logparser/core/LogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ public File exportLogDataToCSV() throws LogDataExportToFileException {

}

/**
* Exports the current LogData to a standard CSV file with a name you give. By default the file will have an escaped version of the Parse
* @param in_fileName a filename to store the CSV export
* @return a CSV file containing the LogData
*/
public File exportLogDataToCSV(String in_fileName) {
T l_firstEntry = this.fetchFirst();

if (l_firstEntry != null) {
return exportLogDataToCSV(l_firstEntry.fetchHeaders(), in_fileName);
} else {
log.warn("No Log data to export. Please load the log data before re-attempting");
return null;
}
}

/**
* Exports the current LogData to a CSV file.
*
Expand All @@ -398,7 +414,7 @@ public File exportLogDataToCSV(Set<String> in_headerSet, String in_csvFileName)
throws LogDataExportToFileException {
File l_exportFile = LogParserFileUtils.createNewFile(in_csvFileName);

try (CSVPrinter printer = new CSVPrinter(new FileWriter(in_csvFileName), CSVFormat.DEFAULT)) {
try (CSVPrinter printer = new CSVPrinter(new FileWriter(l_exportFile), CSVFormat.DEFAULT)) {
printer.printRecord(in_headerSet);

for (StdLogEntry lt_entry : this.getEntries().values()) {
Expand All @@ -410,7 +426,7 @@ public File exportLogDataToCSV(Set<String> in_headerSet, String in_csvFileName)
throw new LogDataExportToFileException("Encountered error while exporting the log data to a CSV file.", ex);
}

return new File(in_csvFileName);
return l_exportFile;
}

/**
Expand Down
Loading

0 comments on commit 688372b

Please sign in to comment.