Skip to content
Permalink
Browse files

feat (#38): Use jansi to produce colorful log messages.

  • Loading branch information...
iocanel committed Jul 31, 2019
1 parent f5b3a60 commit 4b6d1a725bc4a783d17e0220abc0d7ce484dd971
@@ -19,10 +19,10 @@

public interface Logger {

String DEBUG = "[DEBUG] %s";
String INFO = "[INFO] %s";
String WARN = "[WARN] %s.";
String ERROR = "[ERROR] %s";
String DEBUG = "DEBUG";
String INFO = "INFO";
String WARN = "WARN";
String ERROR = "ERROR";

void debug(String message);
void info(String message);
@@ -22,7 +22,7 @@
import java.util.ServiceLoader;
import java.util.stream.StreamSupport;

import io.dekorate.logger.PrintStreamLogger;
import io.dekorate.logger.AnsiLogger;

public abstract class LoggerFactory<C> {

@@ -42,7 +42,7 @@ public static Logger getLogger() {
}
synchronized (LoggerFactory.class) {
if (LOGGER == null) {
LOGGER = new PrintStreamLogger(System.out);
LOGGER = new AnsiLogger();
}
}
return LOGGER;
@@ -21,52 +21,56 @@

import io.dekorate.Logger;
import io.dekorate.LoggerFactory;
import io.dekorate.deps.jansi.*;
import static io.dekorate.deps.jansi.Ansi.*;
import static io.dekorate.deps.jansi.Ansi.Color.*;

public class PrintStreamLogger extends LoggerFactory<PrintStream> implements Logger {
public class AnsiLogger extends LoggerFactory<PrintStream> implements Logger {

private final PrintStream stream;

private final PrintStream stream;

public Logger create(PrintStream stream) {
return new PrintStreamLogger(stream);
return new AnsiLogger(stream);
}

//Should not be used by user code. Only needed for Java SPI.
public PrintStreamLogger() {
this.stream = null;
public AnsiLogger() {
this.stream = AnsiConsole.out;
}

public PrintStreamLogger (PrintStream stream) {
this.stream = stream;
public AnsiLogger (PrintStream stream) {
check();
this.stream = stream != null ? AnsiConsole.wrapPrintStream(stream, 0) : AnsiConsole.out;
}

@Override
public void debug(String message) {
check();
stream.println(String.format(DEBUG, message));
stream.println(ansi().a("[").fg(CYAN).bold().a(DEBUG).reset().a("] ").a(message));
}

@Override
public void info(String message) {
check();
stream.println(String.format(INFO, message));
stream.println(ansi().a("[").fg(BLUE).bold().a(INFO).reset().a("] ").a(message));
}

@Override
public void warning(String message) {
check();
stream.println(String.format(WARN, message));
stream.println(ansi().a("[").fg(MAGENTA).bold().a(WARN).reset().a("] ").a(message));
}

@Override
public void error(String message) {
check();
stream.println(String.format(ERROR, message));
stream.println(ansi().a("[").fg(RED).bold().a(ERROR).reset().a("] ").a(message));
}

private void check() {
if (stream == null) {
throw new IllegalStateException("PrintStreamLogger requires a PrintStream instance.");
throw new IllegalStateException("AnsiLogger requires a PrintStream instance.");
}
}
}
@@ -1,2 +1,2 @@
io.dekorate.logger.AptLogger
io.dekorate.logger.PrintStreamLogger
io.dekorate.logger.AnsiLogger
@@ -53,9 +53,9 @@
</distributionManagement>

<properties>
<version.validation-api />
<version.commons-codec>1.10</version.commons-codec>
<version.commons-compress>1.18</version.commons-compress>
<version.jansi>1.18</version.jansi>
<version.jackson>2.9.7</version.jackson>
<version.kubernetes-client>4.2.2</version.kubernetes-client>
<version.servicecatalog-client>1.1.1</version.servicecatalog-client>
@@ -161,6 +161,12 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- Misc -->
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>${version.jansi}</version>
</dependency>
</dependencies>

<build>
@@ -205,6 +211,7 @@
<include>org.yaml:snakeyaml</include>
<include>org.slf4j:slf4j-api</include>
<include>org.slf4j:slf4j-simple</include>
<include>org.fusesource.jansi:jansi</include>
</includes>
</artifactSet>
<relocations>
@@ -256,6 +263,10 @@
<pattern>org.slf4j</pattern>
<shadedPattern>io.dekorate.deps.org.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>org.fusesource.jansi</pattern>
<shadedPattern>io.dekorate.deps.jansi</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

0 comments on commit 4b6d1a7

Please sign in to comment.
You can’t perform that action at this time.