BAEL-5681 Add colorful logging (#12794)

This commit is contained in:
Shaun Phillips 2022-10-20 18:31:20 +01:00 committed by GitHub
parent 7f3405a765
commit 63bdc428b9
3 changed files with 89 additions and 0 deletions

View File

@ -14,6 +14,14 @@
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-console</finalName>
<resources>

View File

@ -0,0 +1,21 @@
package com.baeldung.color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ColorLogger {
private static final Logger LOGGER = LoggerFactory.getLogger(ColorLogger.class);
public void logDebug(String logging) {
LOGGER.debug("\u001B[34m" + logging + "\u001B[0m");
}
public void logInfo(String logging) {
LOGGER.info("\u001B[32m" + logging + "\u001B[0m");
}
public void logError(String logging) {
LOGGER.error("\u001B[31m" + logging + "\u001B[0m");
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.color;
import static org.fusesource.jansi.Ansi.ansi;
import org.fusesource.jansi.AnsiConsole;
public class PrintColor {
public static void main(String[] args) {
logColorUsingANSICodes();
logColorUsingLogger();
logColorUsingJANSI();
}
private static void logColorUsingANSICodes() {
System.out.println("Here's some text");
System.out.println("\u001B[31m" + "and now the text is red" + "\u001B[0m");
System.out.println("and now back to the default");
}
private static void logColorUsingLogger() {
ColorLogger colorLogger = new ColorLogger();
colorLogger.logDebug("Some debug logging");
colorLogger.logInfo("Some info logging");
colorLogger.logError("Some error logging");
}
private static void logColorUsingJANSI() {
AnsiConsole.systemInstall();
System.out.println(ansi().fgRed().a("Some red text").fgYellow().a(" and some yellow text").reset());
AnsiConsole.systemUninstall();
}
}
/*
* More ANSI codes:
*
* Always conclude your logging with the ANSI reset code: "\u001B[0m"
*
* In each case, replace # with the corresponding number:
*
* 0 = black
* 1 = red
* 2 = green
* 3 = yellow
* 4 = blue
* 5 = purple
* 6 = cyan (light blue)
* 7 = white
*
* \u001B[3#m = color font
* \u001B[4#m = color background
* \u001B[1;3#m = bold font
* \u001B[4;3#m = underlined font
* \u001B[3;3#m = italics font (not widely supported, works in VS Code)
*/