Skip to content

Commit 93bf799

Browse files
authored
Adapted log-parser to include JSON in the commandline execution (#181)
* Adapted log-parser to include JSON in the commandline execution
1 parent 4d655bf commit 93bf799

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ java -jar log-parser-1.11.0.jar --startDir=/path/to/logs --parseDefinition=/path
456456

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

@@ -472,6 +472,7 @@ All reports are stored in the directory `log-parser-reports/export/`.
472472
- **(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).
473473
- **(new feature)** [#117](https://github.com/adobe/log-parser/issues/117) You can now include the file name in the result of the analysis.
474474
- **(new feature)** [#141](https://github.com/adobe/log-parser/issues/141) You can now export a LogData as a table in a HTML file.
475+
- **(new feature)** [#173](https://github.com/adobe/log-parser/issues/173) You can now export a LogData as a JSON file.
475476
- **(new feature)** [#123](https://github.com/adobe/log-parser/issues/123) We now log the total number and size of the parsed files.
476477
- [#110](https://github.com/adobe/log-parser/issues/110) Moved to Java 11
477478
- [#169](https://github.com/adobe/log-parser/issues/169) We only keep one Parse Definition entry with the same title in a Parse Definition.

src/main/java/com/adobe/campaign/tests/logparser/RunLogParser.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,26 @@ public static void main(String[] in_args) throws StringParseException {
6464
l_targetSDKClass);
6565

6666
//Generate Report
67-
if (RunArguments.REPORT_FORMAT.fetchValue(in_args).equalsIgnoreCase("CSV")) {
68-
l_logData.exportLogDataToCSV(RunArguments.REPORT_FILENAME.fetchValue(in_args,
69-
l_parseDefinition.fetchEscapedTitle() + "-export.csv"));
70-
} else if (RunArguments.REPORT_FORMAT.fetchValue(in_args).equalsIgnoreCase("HTML")) {
71-
l_logData.exportLogDataToHTML(
72-
RunArguments.REPORT_NAME.fetchValue(in_args, l_parseDefinition.getTitle()),
73-
RunArguments.REPORT_FILENAME.fetchValue(in_args,
74-
l_parseDefinition.fetchEscapedTitle() + "-export.html"));
75-
} else {
76-
System.err.println("The report format " + RunArguments.REPORT_FORMAT.fetchValue(in_args)
77-
+ " is not supported.");
67+
switch (RunArguments.REPORT_FORMAT.fetchValue(in_args).toUpperCase()) {
68+
case "CSV":
69+
l_logData.exportLogDataToCSV(RunArguments.REPORT_FILENAME.fetchValue(in_args,
70+
l_parseDefinition.fetchEscapedTitle() + "-export.csv"));
71+
break;
72+
case "HTML":
73+
l_logData.exportLogDataToHTML(
74+
RunArguments.REPORT_NAME.fetchValue(in_args, l_parseDefinition.getTitle()),
75+
RunArguments.REPORT_FILENAME.fetchValue(in_args,
76+
l_parseDefinition.fetchEscapedTitle() + "-export.html"));
77+
break;
78+
case "JSON":
79+
l_logData.exportLogDataToJSON(RunArguments.REPORT_FILENAME.fetchValue(in_args,
80+
l_parseDefinition.fetchEscapedTitle() + "-export.json"));
81+
break;
82+
default:
83+
System.err.println("The report format " + RunArguments.REPORT_FORMAT.fetchValue(in_args)
84+
+ " is not supported.");
7885
}
7986

87+
8088
}
8189
}

src/main/java/com/adobe/campaign/tests/logparser/core/LogData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public File exportLogDataToJSON(String in_jsonFileName) throws LogDataExportToFi
518518
T l_firstEntry = this.fetchFirst();
519519

520520
if (l_firstEntry != null) {
521-
return exportLogDataToJSON(l_firstEntry.fetchHeaders(), in_jsonFileName + "-export.json");
521+
return exportLogDataToJSON(l_firstEntry.fetchHeaders(), in_jsonFileName);
522522
} else {
523523
log.warn("No Log data to export. Please load the log data before re-attempting");
524524
return null;

src/main/java/com/adobe/campaign/tests/logparser/utils/RunArguments.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum RunArguments {
2121
"The SDK class to be used for transforming the log data to objects. It is just the class name.",
2222
"com.adobe.campaign.tests.logparser.core.GenericEntry"),
2323
REPORT_FORMAT("reportType", false,
24-
"The format of the report. The allowed values are currently HTML & CSV.", "HTML"),
24+
"The format of the report. The allowed values are currently HTML, JSON & CSV.", "HTML"),
2525
REPORT_FILENAME("reportFileName", false, "The name of the report file. By default, this is the name of the Parse Definition name suffixed with '-export'", ""),
2626
REPORT_NAME("reportName", false, "The report title as show in an HTML report. By default the title includes the Parse Definition name", ""),
2727

src/test/java/com/adobe/campaign/tests/logparser/ExecutionTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.adobe.campaign.tests.logparser.utils.CSVManager;
1616
import com.adobe.campaign.tests.logparser.utils.LogParserFileUtils;
1717
import com.adobe.campaign.tests.logparser.utils.RunArguments;
18+
import com.fasterxml.jackson.databind.ObjectMapper;
1819
import org.hamcrest.Matchers;
1920
import org.testng.annotations.Test;
2021

@@ -95,6 +96,44 @@ public void testSTDMain_printCSV() throws IOException, StringParseException {
9596
}
9697
}
9798

99+
100+
@Test
101+
public void testSTDMain_printJSON() throws IOException, StringParseException {
102+
String l_rootPath = "src/test/resources/nestedDirs/";
103+
String l_fileFilter = "simple*.log";
104+
105+
final String l_jsonPath = "src/test/resources/parseDefinitions/simpleParseDefinitionLogDataFactory.json";
106+
107+
ParseDefinition l_parseDefinition = ParseDefinitionFactory.importParseDefinition(l_jsonPath);
108+
109+
String[] l_args = { RunArguments.START_DIR.buildArgument(l_rootPath),
110+
RunArguments.FILTER_LOG_FILES.buildArgument(l_fileFilter),
111+
RunArguments.PARSE_DEFINITIONS_FILE.buildArgument(l_jsonPath),
112+
RunArguments.REPORT_FORMAT.buildArgument("JSON") };
113+
114+
RunLogParser.main(l_args);
115+
116+
File l_exportedFile = new File(LogParserFileUtils.LOG_PARSER_EXPORTS,
117+
l_parseDefinition.fetchEscapedTitle() + "-export.json");
118+
119+
assertThat("We should successfully create the file", l_exportedFile, notNullValue());
120+
assertThat("We should successfully create the file", l_exportedFile.exists());
121+
assertThat("We should successfully create the file correctly", l_exportedFile.isFile());
122+
try {
123+
assertThat("We successfully created the file correctly", l_exportedFile.getName(),
124+
Matchers.endsWith(l_parseDefinition.fetchEscapedTitle() + "-export.json"));
125+
126+
ObjectMapper objectMapper = new ObjectMapper();
127+
String values = objectMapper.readValue(l_exportedFile, String.class);
128+
129+
assertThat("JSON file contains correct verb definition", values.contains(l_parseDefinition.getDefinitionEntries().get(0).getTitle()));
130+
assertThat("JSON file contains correct api definition", values.contains(l_parseDefinition.getDefinitionEntries().get(1).getTitle()));
131+
132+
} finally {
133+
l_exportedFile.delete();
134+
}
135+
}
136+
98137
@Test
99138
public void testSTDMain_printCSV_empty() throws IOException, StringParseException {
100139
String l_rootPath = "src/test/resources/logTests/acc/";

0 commit comments

Comments
 (0)