我想在spring-shell中着色我的控制台输出。 类似的东西:
System.out.println("\red; red text");
我知道彩色JUL日志记录输出,但它没有用。
我怎么能这样做?
答案 0 :(得分:10)
您可以在输出中使用Ansi值。
这是我的类,有一些颜色常量:
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";
}
您的示例可以通过以下方式生成:
System.out.println(ANSI_RED + "red text" + ANSI_RESET);
答案 1 :(得分:2)
如果您使用AnsiOutput并使用AnsiColor进行Spring Boot应该可以正常工作。例如:
@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);
}
}