代码
SLF4J——Simple Logging Facade For Java,它是一个针对于各类Java日志框架的统一Facade抽象。Java日志框架众多——常用的有java.util.logging, log4j, logback,commons-logging, Spring框架使用的是Jakarta Commons Logging API (JCL)。而SLF4J定义了统一的日志抽象接口,而真正的日志实现则是在运行时决定的——它提供了各类日志框架的binding。
Logback是log4j框架的作者开发的新一代日志框架,它效率更高、能够适应诸多的运行环境,同时天然支持SLF4J。
默认情况下,Spring Boot会用Logback来记录日志
private static final String TAG = "test";
private Logger log = LoggerFactory.getLogger(TAG);
public void test(String str) {
log.debug(str);
log.info(str);
log.warn(str);
log.error(str);
}
无效的方法
IntelliJ IDEA 的版本是当前最新版
网上最容易搜到的方法,就是点击 “Edit Configurations…”,在确保运行模式为 “Spring Boot” 的情况下,将 “Environment” 的 “VM options” 设置为 -Dspring.output.ansi.enabled=ALWAYS
上面的这个方法,等价于在 application.properties
中配置
spring.output.ansi.enabled
- always
- never
- detect
或者在 application.yml 中配置
spring:
output:
ansi:
enabled: always
但是,上面的方法输出的结果还是一样灰蒙蒙的。根本无效。
使用ansi颜色控制代码
下面是控制颜色的代码
// Reset
public static final String RESET = "\033[0m"; // Text Reset
// Regular Colors
public static final String WHITE = "\033[0;30m"; // WHITE
public static final String RED = "\033[0;31m"; // RED
public static final String GREEN = "\033[0;32m"; // GREEN
public static final String YELLOW = "\033[0;33m"; // YELLOW
public static final String BLUE = "\033[0;34m"; // BLUE
public static final String PURPLE = "\033[0;35m"; // PURPLE
public static final String CYAN = "\033[0;36m"; // CYAN
public static final String GREY = "\033[0;37m"; // GREY
// Bold
public static final String WHITE_BOLD = "\033[1;30m"; // WHITE
public static final String RED_BOLD = "\033[1;31m"; // RED
public static final String GREEN_BOLD = "\033[1;32m"; // GREEN
public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW
public static final String BLUE_BOLD = "\033[1;34m"; // BLUE
public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE
public static final String CYAN_BOLD = "\033[1;36m"; // CYAN
public static final String GREY_BOLD = "\033[1;37m"; // GREY
// Underline
public static final String WHITE_UNDERLINED = "\033[4;30m"; // WHITE
public static final String RED_UNDERLINED = "\033[4;31m"; // RED
public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
public static final String GREY_UNDERLINED = "\033[4;37m"; // GREY
// Background
public static final String WHITE_BACKGROUND = "\033[40m"; // WHITE
public static final String RED_BACKGROUND = "\033[41m"; // RED
public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN
public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE
public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN
public static final String GREY_BACKGROUND = "\033[47m"; // GREY
// High Intensity
public static final String WHITE_BRIGHT = "\033[0;90m"; // WHITE
public static final String RED_BRIGHT = "\033[0;91m"; // RED
public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN
public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW
public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE
public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE
public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN
public static final String GREY_BRIGHT = "\033[0;97m"; // GREY
// Bold High Intensity
public static final String WHITE_BOLD_BRIGHT = "\033[1;90m"; // WHITE
public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW
public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE
public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
public static final String GREY_BOLD_BRIGHT = "\033[1;97m"; // GREY
// High Intensity backgrounds
public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;100m";// WHITE
public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
public static final String GREY_BACKGROUND_BRIGHT = "\033[0;107m"; // GREY
在打印日志函数的 message 前面加上颜色定义字符串,在 message 后面加上重置颜色的字符串。为了方便观察,日志改色的效果,这里进行了两次打印来作对比。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Console {
public static Log getInstance(String name) {
return new Log(name);
}
public static class Log {
// private org.apache.commons.logging.Log log;
private Logger log;
public Log(String name) {
// log = LogFactory.getLog(name);
log = LoggerFactory.getLogger(name);
}
public void debug(String str) {
log.debug(BLUE + str + RESET);
log.debug(str);
}
public void info(String str) {
log.info(GREEN + str + RESET);
log.info(str);
}
public void warn(String str) {
log.warn(YELLOW + str + RESET);
log.warn(str);
}
public void error(String str) {
log.error(RED + str + RESET);
log.error(str);
}
}
//此处省略颜色代码的定义
}
通过打印可以看到,并不是整行日志的颜色都会改变。仅有message部分的颜色变了
使用插件
Settings ——> Plugins 中下载 “Grep Console” 插件
下载完后,会提示 “重启” ,重启之后,需要在设置中配置字体颜色。
这里可以根据 “亮色” 和 “暗色” 的主题进行分别配色
配置后重新运行程序,可以看到输出的日志已经全部变色了