Skip to content

Commit 63bdc42

Browse files
authored
BAEL-5681 Add colorful logging (eugenp#12794)
1 parent 7f3405a commit 63bdc42

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

core-java-modules/core-java-console/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
<version>0.0.1-SNAPSHOT</version>
1515
</parent>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>org.fusesource.jansi</groupId>
20+
<artifactId>jansi</artifactId>
21+
<version>2.4.0</version>
22+
</dependency>
23+
</dependencies>
24+
1725
<build>
1826
<finalName>core-java-console</finalName>
1927
<resources>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.color;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class ColorLogger {
7+
8+
private static final Logger LOGGER = LoggerFactory.getLogger(ColorLogger.class);
9+
10+
public void logDebug(String logging) {
11+
LOGGER.debug("\u001B[34m" + logging + "\u001B[0m");
12+
}
13+
14+
public void logInfo(String logging) {
15+
LOGGER.info("\u001B[32m" + logging + "\u001B[0m");
16+
}
17+
18+
public void logError(String logging) {
19+
LOGGER.error("\u001B[31m" + logging + "\u001B[0m");
20+
}
21+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.color;
2+
3+
import static org.fusesource.jansi.Ansi.ansi;
4+
5+
import org.fusesource.jansi.AnsiConsole;
6+
7+
public class PrintColor {
8+
9+
public static void main(String[] args) {
10+
logColorUsingANSICodes();
11+
12+
logColorUsingLogger();
13+
14+
logColorUsingJANSI();
15+
}
16+
17+
private static void logColorUsingANSICodes() {
18+
System.out.println("Here's some text");
19+
System.out.println("\u001B[31m" + "and now the text is red" + "\u001B[0m");
20+
System.out.println("and now back to the default");
21+
}
22+
23+
private static void logColorUsingLogger() {
24+
ColorLogger colorLogger = new ColorLogger();
25+
colorLogger.logDebug("Some debug logging");
26+
colorLogger.logInfo("Some info logging");
27+
colorLogger.logError("Some error logging");
28+
}
29+
30+
private static void logColorUsingJANSI() {
31+
AnsiConsole.systemInstall();
32+
33+
System.out.println(ansi().fgRed().a("Some red text").fgYellow().a(" and some yellow text").reset());
34+
35+
AnsiConsole.systemUninstall();
36+
}
37+
}
38+
39+
/*
40+
* More ANSI codes:
41+
*
42+
* Always conclude your logging with the ANSI reset code: "\u001B[0m"
43+
*
44+
* In each case, replace # with the corresponding number:
45+
*
46+
* 0 = black
47+
* 1 = red
48+
* 2 = green
49+
* 3 = yellow
50+
* 4 = blue
51+
* 5 = purple
52+
* 6 = cyan (light blue)
53+
* 7 = white
54+
*
55+
* \u001B[3#m = color font
56+
* \u001B[4#m = color background
57+
* \u001B[1;3#m = bold font
58+
* \u001B[4;3#m = underlined font
59+
* \u001B[3;3#m = italics font (not widely supported, works in VS Code)
60+
*/

0 commit comments

Comments
 (0)