效果:
设置控制台打印字体颜色、背景、字体样式,java工具类
package org.dxl.log;
import java.util.Arrays;
/**
* 在控制台按照传入格式输出
*
* @author IT_CREAT
* @date 2021 2021/1/17/017 4:49
*/
public class ColorPrint {
/**
* 分号
*/
private static final String SEMICOLON = ";";
/**
* 默认黑色打印
*
* @param txt 信息
*/
public static void outPrintln(String txt) {
System.out.println(format(txt, PrintCode.BLACK));
}
/**
* 换行打印
*
* @param txt 信息
* @param codes 格式化参数
*/
public static void outPrintln(String txt, PrintCode... codes) {
System.out.println(format(txt, codes));
}
/**
* 不换行打印
*
* @param txt 打印内容
* @param codes 格式化参数
*/
public static void outPrint(String txt, PrintCode... codes) {
System.out.print(format(txt, codes));
}
/**
* 默认红色打印
*
* @param txt 信息
*/
public static void errorPrintln(String txt) {
System.err.println(format(txt, PrintCode.RED));
}
/**
* 换行打印
*
* @param txt 信息
* @param codes 格式化参数
*/
public static void errorPrintln(String txt, PrintCode... codes) {
System.err.println(format(txt, codes));
}
/**
* 不换行打印
*
* @param txt 打印内容
* @param codes 格式化参数
*/
public static void errorPrint(String txt, PrintCode... codes) {
System.err.print(format(txt, codes));
}
/**
* 格式化信息
*
* @param txt 信息
* @param codes 参数集合
* @return 格式化后的信息
*/
private static String format(String txt, PrintCode... codes) {
String codeStr = String.join(SEMICOLON, Arrays.stream(codes).map((printCode) -> String.valueOf(printCode.getCode())).toArray(String[]::new));
return (char) 27 + "[" + codeStr + "m" + txt + (char) 27 + "[0m";
}
/**
* 打印样例
*/
public static void printExample() {
ColorPrint.outPrintln("样例提示:黑色字体和黑色背景是一个取反色,和整体控制台主题背景有关,主题背景如果为深色,则相应的字体和背景变为白色,反之黑色",
ColorPrint.PrintCode.BOLD, ColorPrint.PrintCode.UNDERLINE);
ColorPrint.outPrintln("");
ColorPrint.outPrintln("灰色(ColorPrint.PrintCode.GREY)", ColorPrint.PrintCode.GREY);
ColorPrint.outPrintln("黑色(ColorPrint.PrintCode.BLACK)", ColorPrint.PrintCode.BLACK);
ColorPrint.outPrintln("红色(ColorPrint.PrintCode.RED)", ColorPrint.PrintCode.RED);
ColorPrint.outPrintln("绿色(ColorPrint.PrintCode.GREEN)", ColorPrint.PrintCode.GREEN);
ColorPrint.outPrintln("黄色(ColorPrint.PrintCode.YELLOW)", ColorPrint.PrintCode.YELLOW);
ColorPrint.outPrintln("蓝色(ColorPrint.PrintCode.BLUE)", ColorPrint.PrintCode.BLUE);
ColorPrint.outPrintln("品红(ColorPrint.PrintCode.MAGENTA)", ColorPrint.PrintCode.MAGENTA);
ColorPrint.outPrintln("蓝绿(ColorPrint.PrintCode.CYAN)", ColorPrint.PrintCode.CYAN);
ColorPrint.outPrintln("黑色背景(ColorPrint.PrintCode.BLACK_BACKGROUND)",
ColorPrint.PrintCode.GREY, ColorPrint.PrintCode.BLACK_BACKGROUND);
ColorPrint.outPrintln("红色背景(ColorPrint.PrintCode.RED_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.RED_BACKGROUND);
ColorPrint.outPrintln("绿色背景(ColorPrint.PrintCode.GREEN_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.GREEN_BACKGROUND);
ColorPrint.outPrintln("黄色背景(ColorPrint.PrintCode.YELLOW_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.YELLOW_BACKGROUND);
ColorPrint.outPrintln("蓝色背景(ColorPrint.PrintCode.BLUE_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.BLUE_BACKGROUND);
ColorPrint.outPrintln("品红背景(ColorPrint.PrintCode.MAGENTA_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.MAGENTA_BACKGROUND);
ColorPrint.outPrintln("蓝绿背景(ColorPrint.PrintCode.CYAN_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.CYAN_BACKGROUND);
ColorPrint.outPrintln("灰色背景(ColorPrint.PrintCode.GREY_BACKGROUND)",
ColorPrint.PrintCode.BLACK, ColorPrint.PrintCode.GREY_BACKGROUND);
ColorPrint.outPrintln("默认字体(不传入其它参数)");
ColorPrint.outPrintln("加粗(ColorPrint.PrintCode.BOLD)", ColorPrint.PrintCode.BOLD);
ColorPrint.outPrintln("斜体(ColorPrint.PrintCode.ITALIC)", ColorPrint.PrintCode.ITALIC);
ColorPrint.outPrintln("下划线(ColorPrint.PrintCode.UNDERLINE)", ColorPrint.PrintCode.UNDERLINE);
ColorPrint.outPrintln(
"示例:灰底/黑字/加粗/下划线/倾斜(ColorPrint.PrintCode.GREY_BACKGROUND," +
"ColorPrint.PrintCode.BLACK,ColorPrint.PrintCode.BOLD," +
"ColorPrint.PrintCode.UNDERLINE,ColorPrint.PrintCode.ITALIC)",
ColorPrint.PrintCode.GREY_BACKGROUND, ColorPrint.PrintCode.BLACK,
ColorPrint.PrintCode.BOLD, ColorPrint.PrintCode.UNDERLINE, ColorPrint.PrintCode.ITALIC);
}
/**
* 控制台信息格式化参数
*/
public enum PrintCode {
/**
* 黑色
*/
BLACK(30),
/**
* 黑色背景
*/
BLACK_BACKGROUND(40),
/**
* 红色
*/
RED(31),
/**
* 红色背景
*/
RED_BACKGROUND(41),
/**
* 绿色
*/
GREEN(32),
/**
* 绿色背景
*/
GREEN_BACKGROUND(42),
/**
* 黄色
*/
YELLOW(33),
/**
* 黄色背景
*/
YELLOW_BACKGROUND(43),
/**
* 蓝色
*/
BLUE(34),
/**
* 蓝色背景
*/
BLUE_BACKGROUND(44),
/**
* 品红(洋红)
*/
MAGENTA(35),
/**
* 品红背景
*/
MAGENTA_BACKGROUND(45),
/**
* 蓝绿
*/
CYAN(36),
/**
* 蓝绿背景
*/
CYAN_BACKGROUND(46),
/**
* 灰色
*/
GREY(37),
/**
* 灰色背景
*/
GREY_BACKGROUND(47),
/**
* 粗体
*/
BOLD(1),
/**
* 斜体
*/
ITALIC(3),
/**
* 下划线
*/
UNDERLINE(4);
private final int code;
PrintCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
}
Grep Console
或者安装插件:Grep Console(主要针对日志打印,插件是对打印内容正则匹配)
该插件是利用正则进行匹配,比如默认配置中的关键字,也就是Expression这一列,默认是大写的日志级别关键字,所以我们只需要在打印的字符中含有关键字,就会匹配到
public static void main(String[] args) {
System.out.println("测试警告[WARN]");
}
效果:
插件搜索安装:settings–》plugins
设置样式: