Skip to content

Commit

Permalink
feat(Parsing): Support parsing arbitrary status in parse rule file
Browse files Browse the repository at this point in the history
  • Loading branch information
hypery2k authored Jul 1, 2019
2 parents aedc348 + 95bd180 commit 846351d
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 46 deletions.
11 changes: 11 additions & 0 deletions src/main/java/hudson/plugins/logparser/CompiledPatterns.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package hudson.plugins.logparser;

import javax.annotation.CheckForNull;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class CompiledPatterns {

private String errorMsg;
private Pattern[] compiledPatterns;
private List<String> extraTags;

public CompiledPatterns() {
this.errorMsg = null;
this.compiledPatterns = null;
this.extraTags = new ArrayList<String>();
}

public String getError() {
Expand All @@ -34,4 +38,11 @@ public void setCompiledPatters(final Pattern[] compiledPatterns) {
this.compiledPatterns = compiledPatterns;
}

public List<String> getExtraTags() {
return extraTags;
}

public void setExtraTags(List<String> extraTags) {
this.extraTags = extraTags;
}
}
9 changes: 9 additions & 0 deletions src/main/java/hudson/plugins/logparser/LogParserAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ private CategoryDataset buildDataSet() {
new ChartUtil.NumberOnlyBuildLabel(a.getOwner()));
dsb.add(a.result.getTotalInfos(), "infos",
new ChartUtil.NumberOnlyBuildLabel(a.getOwner()));
dsb.add(a.result.getTotalDebugs(), "debugs",
new ChartUtil.NumberOnlyBuildLabel(a.getOwner()));
for (String extraTag : a.result.getExtraTags()) {
dsb.add(a.result.getTotalCountsByExtraTag(extraTag), extraTag,
new ChartUtil.NumberOnlyBuildLabel(a.getOwner()));
}
}
return dsb.build();
}
Expand Down Expand Up @@ -211,6 +217,8 @@ public String generateToolTip(CategoryDataset dataset, int row,
return "Errors: " + result.getTotalErrors();
case 1:
return "Warnings: " + result.getTotalWarnings();
case 2:
return "Debugs: " + result.getTotalDebugs();
default:
return "Infos: " + result.getTotalInfos();
}
Expand All @@ -220,6 +228,7 @@ public String generateToolTip(CategoryDataset dataset, int row,
ar.setSeriesPaint(0, ColorPalette.RED); // error
ar.setSeriesPaint(1, ColorPalette.BLUE); // info
ar.setSeriesPaint(2, ColorPalette.YELLOW); // warning
ar.setSeriesPaint(3, ColorPalette.BLUE); // debug

// crop extra space around the graph
plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/hudson/plugins/logparser/LogParserConsts.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class LogParserConsts {
public static final String ERROR = "ERROR";
public static final String WARNING = "WARNING";
public static final String INFO = "INFO";
public static final String DEBUG = "DEBUG";
public static final String NONE = "NONE";
public static final String START = "START"; // marks a beginning of a section
public static final String DEFAULT = NONE;
Expand All @@ -18,9 +19,9 @@ public class LogParserConsts {
public static final String CANNOT_PARSE = "log-parser plugin ERROR: Cannot parse log ";
public static final String NOT_INT = " is not an integer - using default";

public static final List<String> LEGAL_STATUS = Arrays.asList(ERROR, WARNING, INFO, NONE, START);
public static final List<String> STATUSES_WITH_LINK_FILES = Arrays.asList(ERROR, WARNING, INFO);
public static final List<String> STATUSES_WITH_SECTIONS_IN_LINK_FILES = Arrays.asList(ERROR, WARNING);
public static final List<String> LEGAL_STATUS = Arrays.asList(ERROR, WARNING, INFO, DEBUG, NONE, START);
public static final List<String> STATUSES_WITH_LINK_FILES = Arrays.asList(ERROR, WARNING, INFO, DEBUG);
public static final List<String> STATUSES_WITH_SECTIONS_IN_LINK_FILES = Arrays.asList(ERROR, WARNING, DEBUG);

public static String getHtmlOpeningTags() {
final String hudsonRoot = Jenkins.getActiveInstance().getRootUrl();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/hudson/plugins/logparser/LogParserDisplayConsts.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hudson.plugins.logparser;

import java.util.HashMap;
import org.apache.commons.lang.WordUtils;

public class LogParserDisplayConsts {

Expand All @@ -9,26 +10,41 @@ public class LogParserDisplayConsts {
final private HashMap<String, String> linkListDisplay = new HashMap<String, String>();
final private HashMap<String, String> linkListDisplayPlural = new HashMap<String, String>();

public static final String DEFAULT_COLOR = "blue";
public static final String DEFAULT_ICON = "blue.gif";

public static String getDefaultLinkListDisplay(String status) {
return WordUtils.capitalize(status);
}

public static String getDefaultLinkListDisplayPlural(String status) {
return getDefaultLinkListDisplay(status) + "s";
}

public LogParserDisplayConsts() {
// Color of each status
colorTable.put(LogParserConsts.ERROR, "red");
colorTable.put(LogParserConsts.WARNING, "orange");
colorTable.put(LogParserConsts.INFO, "blue");
colorTable.put(LogParserConsts.START, "blue");
colorTable.put(LogParserConsts.DEBUG, "blue");

// Icon for each status in the summary
iconTable.put(LogParserConsts.ERROR, "red.gif");
iconTable.put(LogParserConsts.WARNING, "yellow.gif");
iconTable.put(LogParserConsts.INFO, "blue.gif");
iconTable.put(LogParserConsts.DEBUG, "blue.gif");

// How to display in link summary html
linkListDisplay.put(LogParserConsts.ERROR, "Error");
linkListDisplay.put(LogParserConsts.WARNING, "Warning");
linkListDisplay.put(LogParserConsts.INFO, "Info");
linkListDisplay.put(LogParserConsts.DEBUG, "Debug");

linkListDisplayPlural.put(LogParserConsts.ERROR, "Errors");
linkListDisplayPlural.put(LogParserConsts.WARNING, "Warnings");
linkListDisplayPlural.put(LogParserConsts.INFO, "Infos");
linkListDisplayPlural.put(LogParserConsts.DEBUG, "Debugs");
}

public HashMap<String, String> getColorTable() {
Expand Down
71 changes: 53 additions & 18 deletions src/main/java/hudson/plugins/logparser/LogParserParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
Expand All @@ -29,6 +31,7 @@ public class LogParserParser {
final private String[] parsingRulesArray;
final private Pattern[] compiledPatterns;
final private CompiledPatterns compiledPatternsPlusError;
final private List<String> extraTags;

// if key is 3-ERROR it shows how many errors are in section 3
final private HashMap<String, Integer> statusCountPerSection = new HashMap<String, Integer>();
Expand All @@ -47,11 +50,6 @@ public LogParserParser(final FilePath parsingRulesFile,
// init logger
final Logger logger = Logger.getLogger(getClass().getName());

// Count of lines in this status
statusCount.put(LogParserConsts.ERROR, 0);
statusCount.put(LogParserConsts.WARNING, 0);
statusCount.put(LogParserConsts.INFO, 0);

this.parsingRulesArray = LogParserUtils
.readParsingRules(parsingRulesFile);

Expand All @@ -61,9 +59,19 @@ public LogParserParser(final FilePath parsingRulesFile,
this.parsingRulesArray, logger);
this.compiledPatterns = this.compiledPatternsPlusError
.getCompiledPatterns();
this.extraTags = this.compiledPatternsPlusError.getExtraTags();

this.preformattedHtml = preformattedHtml;
this.channel = channel;

// Count of lines in this status
statusCount.put(LogParserConsts.ERROR, 0);
statusCount.put(LogParserConsts.WARNING, 0);
statusCount.put(LogParserConsts.INFO, 0);
statusCount.put(LogParserConsts.DEBUG, 0);
for (String extraTag : this.extraTags) {
statusCount.put(extraTag, 0);
}
}

/*
Expand All @@ -90,16 +98,24 @@ public LogParserResult parseLog(final Run<?, ?> build) throws IOException, Inter
// Determine parsed log files
final String parsedFilePath = logDirectory + "/log_content.html";
final String errorLinksFilePath = logDirectory + "/logerrorLinks.html";
final String warningLinksFilePath = logDirectory
+ "/logwarningLinks.html";
final String warningLinksFilePath = logDirectory + "/logwarningLinks.html";
final String infoLinksFilePath = logDirectory + "/loginfoLinks.html";
final String debugLinksFilePath = logDirectory + "/logdebugLinks.html";
final Map<String, String> linksFilePathByExtraTags = new HashMap<String, String>();
for (String extraTag : this.extraTags) {
linksFilePathByExtraTags.put(extraTag, logDirectory + "/log" + extraTag + "Links.html");
}
final String buildRefPath = logDirectory + "/log_ref.html";
final String buildWrapperPath = logDirectory + "/log.html";

// Record file paths in hash
// Record file paths in HashMap
linkFiles.put(LogParserConsts.ERROR, errorLinksFilePath);
linkFiles.put(LogParserConsts.WARNING, warningLinksFilePath);
linkFiles.put(LogParserConsts.INFO, infoLinksFilePath);
linkFiles.put(LogParserConsts.DEBUG, debugLinksFilePath);
for (String extraTag : this.extraTags) {
linkFiles.put(extraTag, linksFilePathByExtraTags.get(extraTag));
}

// Open console log for reading and all other files for writing
final BufferedWriter writer = new BufferedWriter(new FileWriter(
Expand All @@ -112,18 +128,23 @@ public LogParserResult parseLog(final Run<?, ?> build) throws IOException, Inter
warningLinksFilePath)));
writers.put(LogParserConsts.INFO, new BufferedWriter(new FileWriter(
infoLinksFilePath)));
writers.put(LogParserConsts.DEBUG, new BufferedWriter(new FileWriter(
debugLinksFilePath)));
for (String extraTag : this.extraTags) {
writers.put(extraTag, new BufferedWriter(new FileWriter(
linksFilePathByExtraTags.get(extraTag))));
}

// Loop on the console log as long as there are input lines and parse
// line by line
// At the end of this loop, we will have:
// - a parsed log with colored lines
// - 3 links files which will be consolidated into one referencing html
// - 4 links files which will be consolidated into one referencing html
// file.

// Create dummy header and section for beginning of log
final String shortLink = " <a target=\"content\" href=\"log_content.html\">Beginning of log</a>";
LogParserWriter.writeHeaderTemplateToAllLinkFiles(writers,
sectionCounter); // This enters a line which will later be
LogParserWriter.writeHeaderTemplateToAllLinkFiles(writers, sectionCounter); // This enters a line which will later be
// replaced by the actual header and count for
// this header
headerForSection.add(shortLink);
Expand Down Expand Up @@ -156,14 +177,18 @@ public LogParserResult parseLog(final Run<?, ?> build) throws IOException, Inter
((BufferedWriter) writers.get(LogParserConsts.ERROR)).close();
((BufferedWriter) writers.get(LogParserConsts.WARNING)).close();
((BufferedWriter) writers.get(LogParserConsts.INFO)).close();
((BufferedWriter) writers.get(LogParserConsts.DEBUG)).close();
for (String extraTag : this.extraTags) {
((BufferedWriter) writers.get(extraTag)).close();
}

// Build the reference html from the warnings/errors/info html files
// created in the loop above
LogParserWriter.writeReferenceHtml(buildRefPath, headerForSection,
statusCountPerSection, displayConstants.getIconTable(),
displayConstants.getLinkListDisplay(),
displayConstants.getLinkListDisplayPlural(), statusCount,
linkFiles);
linkFiles, extraTags);
// Write the wrapping html for the reference page and the parsed log page
LogParserWriter.writeWrapperHtml(buildWrapperPath);

Expand All @@ -175,16 +200,23 @@ public LogParserResult parseLog(final Run<?, ?> build) throws IOException, Inter
final LogParserResult result = new LogParserResult();
result.setHtmlLogFile(parsedFilePath);
result.setTotalErrors((Integer) statusCount.get(LogParserConsts.ERROR));
result.setTotalWarnings((Integer) statusCount
.get(LogParserConsts.WARNING));
result.setTotalWarnings((Integer) statusCount.get(LogParserConsts.WARNING));
result.setTotalInfos((Integer) statusCount.get(LogParserConsts.INFO));
result.setTotalDebugs((Integer) statusCount.get(LogParserConsts.DEBUG));
for (String extraTag : this.extraTags) {
result.putTotalCountsByExtraTag(extraTag, (Integer) statusCount.get(extraTag));
}
result.setErrorLinksFile(errorLinksFilePath);
result.setWarningLinksFile(warningLinksFilePath);
result.setInfoLinksFile(infoLinksFilePath);
result.setDebugLinksFile(debugLinksFilePath);
for (String extraTag : this.extraTags) {
result.putLinksFileByExtraTag(extraTag, linksFilePathByExtraTags.get(extraTag));
}
result.setParsedLogURL(parsedLogURL);
result.setHtmlLogPath(logDirectory);
result.setBadParsingRulesError(this.compiledPatternsPlusError
.getError());
result.setBadParsingRulesError(this.compiledPatternsPlusError.getError());
result.setExtraTags(this.extraTags);

return result;

Expand Down Expand Up @@ -250,10 +282,13 @@ public void incrementCounterPerSection(final String status,
}

private String colorLine(final String line, final String status) {
final String color = (String) displayConstants.getColorTable().get(status);
String color = (String) displayConstants.getColorTable().get(status);
if (color == null) {
color = LogParserDisplayConsts.DEFAULT_COLOR;
}
final StringBuffer result = new StringBuffer("<span class=\"");
result.append(status.toLowerCase());
result.append("\" style=\"color:");
result.append("\" style=\"color: ");
result.append(color);
result.append("\">");
result.append(line);
Expand Down
Loading

0 comments on commit 846351d

Please sign in to comment.