Skip to content

Commit

Permalink
(#7) add message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jpommerening committed Aug 4, 2017
1 parent a1f67d4 commit 2e57132
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Binary file modified java/com/aixigo/pdfreactor_loader/DefaultLogWriter$Color.class
Binary file not shown.
Binary file modified java/com/aixigo/pdfreactor_loader/DefaultLogWriter.class
Binary file not shown.
39 changes: 26 additions & 13 deletions java/com/aixigo/pdfreactor_loader/DefaultLogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

public class DefaultLogWriter implements LogWriter {

private static String JSLOG_PREFIX = "JSlog:";
private static String JSERROR_PREFIX = "JSerror:";

private enum Color {
RESET( "\033[0m" ),
BOLD( "\033[1m" ),
Expand All @@ -32,21 +35,19 @@ public String toString() {
}

public void writeMessage( LogRecord record ) {
String message = record.getMessage();
String rawMessage = record.getMessage();
String time = formatMillis( record.getMillis() );
String level = formatLevel( record.getLevel() );
boolean isJsLog = message.startsWith( "JSlog:" );
boolean isJsError = message.startsWith( "JSerror:" );

System.out.print(
"PDF: " +
time +
"\t" +
( ( isJsLog || isJsError ) ? colorize( Color.BGBLUE, level ) : level ) +
"\t" +
message +
"\n"
);
String message = formatMessage( rawMessage );

boolean isJsLog = rawMessage.startsWith( JSLOG_PREFIX );
boolean isJsError = rawMessage.startsWith( JSERROR_PREFIX );

if( isJsLog || isJsError ) {
level = colorize( Color.BGBLUE, level );
}

System.out.print( "PDF: " + time + "\t" + level + "\t" + message + "\n" );
}

private String formatMillis( long millis ) {
Expand All @@ -65,6 +66,18 @@ private String formatLevel( Level level ) {
return level.getName();
}

private String formatMessage( String message ) {
if( message.startsWith( JSLOG_PREFIX ) ) {
return colorize( Color.BOLD, JSLOG_PREFIX.toUpperCase() ) +
message.substring( JSLOG_PREFIX.length() );
}
if( message.startsWith( JSERROR_PREFIX ) ) {
return colorize( Color.RED, JSERROR_PREFIX.toUpperCase() ) +
message.substring( JSERROR_PREFIX.length() );
}
return message;
}

private String colorize( Color color, String string ) {
return "" + color + string + Color.RESET;
}
Expand Down

0 comments on commit 2e57132

Please sign in to comment.