I want to colorize my console output in spring-shell. Something like:
System.out.println("\red; red text");
I know about colorized JUL logging output, but it's not enought.
How I can do that?
You can use Ansi values in your output.
Here is my class with some color constants:
public class AnsiConstants {
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_GRAY = "\u001B[37m";
public static final String ANSI_WHITE = "\u001B[37;1m";
}
Your example can be generated with:
System.out.println(ANSI_RED + "red text" + ANSI_RESET);
If you are based on Spring Boot using AnsiOutput with AnsiColor should work. Example:
@SpringBootApplication
public class Application {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
System.out.println(AnsiOutput.toString(
AnsiColor.GREEN, "Service started successfully", AnsiColor.DEFAULT));
}
@EventListener
public void onApplicationFailed(ApplicationFailedEvent event) {
System.err.println(AnsiOutput.toString(
AnsiColor.RED, "Service startup failed", AnsiColor.DEFAULT));
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With