Skip to content

Commit

Permalink
Refactor and cleanup, add unit test for log colors
Browse files Browse the repository at this point in the history
  • Loading branch information
evie-lau committed Jun 29, 2023
1 parent 03c4a8b commit a9932c6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
60 changes: 27 additions & 33 deletions src/main/java/io/openliberty/tools/ant/AbstractTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@
public abstract class AbstractTask extends Task {

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";

protected File installDir;
protected File userDir;
Expand All @@ -62,7 +56,7 @@ public abstract class AbstractTask extends Task {

protected ProcessBuilder processBuilder;

private static final String LIBERTY_MESSAGE_TYPE_REGEX = ".*\\[.*\\] [\\S]*?(\\S):.*";
private static final String LIBERTY_MESSAGE_TYPE_REGEX = ".*\\[.*\\]\\s*[\\S]*?(\\S):.*";
private static final Pattern LIBERTY_MESSAGE_TYPE_PATTERN = Pattern.compile(LIBERTY_MESSAGE_TYPE_REGEX);

protected static final String DEFAULT_SERVER = "defaultServer";
Expand Down Expand Up @@ -263,6 +257,31 @@ public void sendErrorInvokeCommand(String commandLine, int returnCode, int... ex
Arrays.toString(expectedExitCodes)));
}

/**
* Check the last letter of Liberty server message code to determine severity, and log with appropriate color
* @param line - Liberty server message
*/
public static String addColor(String line) {
Matcher m = LIBERTY_MESSAGE_TYPE_PATTERN.matcher(line);
// Group 1 - liberty log identifier code
String color = null;
if (m.find()) {
String identifier = m.group(1);
switch (identifier) {
case "E":
color = ANSI_RED;
break;
case "W":
color = ANSI_YELLOW;
break;
}
}
if (color != null) {
line = color + line + ANSI_RESET;
}
return line;
}

private class StreamCopier extends Thread {
private final BufferedReader reader;
private boolean joined;
Expand All @@ -287,7 +306,7 @@ public void run() {
break;
}
sb.append(line);
logWithColor(line);
log(addColor(line));
}
}
} catch (IOException ex) {
Expand All @@ -307,31 +326,6 @@ public void run() {
}
}

/**
* Check the last letter of Liberty server message code to determine severity, and log with appropriate color
* @param line - Liberty server message
*/
private void logWithColor(String line) {
Matcher m = LIBERTY_MESSAGE_TYPE_PATTERN.matcher(line);
// Group 1 - liberty log identifier code
if (m.find()) {
String identifier = m.group(1);
switch (identifier) {
case "E":
log(ANSI_RED + line + ANSI_RESET);
break;
case "W":
log(ANSI_YELLOW + line + ANSI_RESET);
break;
default:
log(line);
break;
}
} else { // could not find Liberty server message identifier
log(line);
}
}

public String getOutput() {
if (sb != null) {
return sb.toString();
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/net/wasdev/wlp/ant/LogColorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.wasdev.wlp.ant;

import org.junit.Assert;
import org.junit.Test;
import io.openliberty.tools.ant.AbstractTask;

public class LogColorTest {
@Test
public void testWarningColor() {
String logMessage = "[WARNING ] CWWKS3103W: There are no users defined for the BasicRegistry configuration of ID com.ibm.ws.security.registry.basic.config[basic].";
String line = AbstractTask.addColor(logMessage);
Assert.assertTrue(line.contains(AbstractTask.ANSI_YELLOW));
Assert.assertTrue(line.contains(AbstractTask.ANSI_RESET));
}

@Test
public void testErrorColor() {
String logMessage = "[ERROR ] SRVE9990E: The class mvn.demo.rest.RestApplication has a @WebServlet annotation but does not implement the jakarta.servlet.http.HttpServlet interface.";
String line = AbstractTask.addColor(logMessage);
Assert.assertTrue(line.contains(AbstractTask.ANSI_RED));
Assert.assertTrue(line.contains(AbstractTask.ANSI_RESET));
}

@Test
public void testInfoColor() {
String logMessage = "[AUDIT ] CWWKT0016I: Web application available (default_host): http://localhost:9080/openapi/";
String line = AbstractTask.addColor(logMessage);
Assert.assertEquals(logMessage, line); // should not have any ANSI colors
}}

0 comments on commit a9932c6

Please sign in to comment.