From 3c6067d5bf6ceea41b50ada17f562c01ec18c726 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 27 Apr 2022 14:57:46 +0800 Subject: [PATCH] add ConsoleColorLog --- CHANGELOG.md | 2 + .../log/dialect/console/ConsoleColorLog.java | 188 ++++-------------- .../console/ConsoleColorLogFactory.java | 7 +- .../log/dialect/console/ConsoleLog.java | 2 +- .../hutool/log/dialect/tinylog/TinyLog2.java | 16 +- .../cn/hutool/log/test/StaticLogTest.java | 6 +- 6 files changed, 56 insertions(+), 165 deletions(-) mode change 100644 => 100755 hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLog.java mode change 100644 => 100755 hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLogFactory.java mode change 100644 => 100755 hutool-log/src/test/java/cn/hutool/log/test/StaticLogTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index bb996b424..5d77228fa 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ * 【core 】 ReflectUtil.newInstanceIfPossible添加枚举、数组等类型的默认实现 * 【core 】 CombinationAnnotationElement增加过滤(pr#605@Gitee) * 【all 】 精简CHANGELOG +* 【core 】 新增AnsiEncoder +* 【log 】 新增彩色日式输出风格ConsoleColorLog(pr#607@Gitee) ### 🐞Bug修复 * 【core 】 修复StrUtil.firstNonX非static问题(issue#2257@Github) diff --git a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLog.java b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLog.java old mode 100644 new mode 100755 index f99404376..3f5495ea8 --- a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLog.java +++ b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLog.java @@ -1,76 +1,61 @@ package cn.hutool.log.dialect.console; -import cn.hutool.core.date.DatePattern; -import cn.hutool.core.lang.Assert; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.lang.ansi.AnsiColor; +import cn.hutool.core.lang.ansi.AnsiEncoder; import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.log.AbstractLog; import cn.hutool.log.level.Level; -import java.util.Date; +import java.util.function.Function; /** - * @author hongda.li 2022-04-27 09:55 + * 利用System.out.println()打印彩色日志 + * + * @author hongda.li, looly + * @since 5.8.0 */ -public class ConsoleColorLog extends AbstractLog { - - //-----------------------------------可供定制的不同级别的日志颜色代码--------------------------- - - private static int ERROR = 31; - private static int INFO = 32; - private static int DEBUG = 32; - private static int WARN = 33; - private static int TRACE = 35; - - public static void setErrorColor(int errorColor) { - ConsoleColorLog.ERROR = errorColor; - } - - public static void setInfoColor(int infoColor) { - ConsoleColorLog.INFO = infoColor; - } - - public static void setDebugColor(int debugColor) { - ConsoleColorLog.DEBUG = debugColor; - } - - public static void setWarnColor(int warnColor) { - ConsoleColorLog.WARN = warnColor; - } - - public static void setTraceColor(int traceColor) { - ConsoleColorLog.TRACE = traceColor; - } +public class ConsoleColorLog extends ConsoleLog { /** * 控制台打印类名的颜色代码 */ - private static final int CLASSNAME = 36; + private static final AnsiColor COLOR_CLASSNAME = AnsiColor.CYAN; /** * 控制台打印时间的颜色代码 */ - private static final int TIME = 37; + private static final AnsiColor COLOR_TIME = AnsiColor.WHITE; /** * 控制台打印正常信息的颜色代码 */ - private static final int NONE = 38; + private static final AnsiColor COLOR_NONE = AnsiColor.DEFAULT; + + private static Function colorFactory = (level -> { + switch (level) { + case DEBUG: + case INFO: + return AnsiColor.GREEN; + case WARN: + return AnsiColor.YELLOW; + case ERROR: + return AnsiColor.RED; + case TRACE: + return AnsiColor.MAGENTA; + default: + return COLOR_NONE; + } + }); /** - * 系统换行符 + * 设置颜色工厂,根据日志级别,定义不同的颜色 + * + * @param colorFactory 颜色工厂函数 */ - private static final String LINE_SEPARATOR = System.lineSeparator(); - - /** - * 日志名称 - */ - private final String name; - - /** - * 日志级别 - */ - private static Level currentLevel = Level.DEBUG; + public static void setColorFactory(Function colorFactory) { + ConsoleColorLog.colorFactory = colorFactory; + } /** * 构造 @@ -78,7 +63,7 @@ public class ConsoleColorLog extends AbstractLog { * @param name 类名 */ public ConsoleColorLog(String name) { - this.name = name; + super(name); } /** @@ -87,111 +72,16 @@ public class ConsoleColorLog extends AbstractLog { * @param clazz 类 */ public ConsoleColorLog(Class clazz) { - this.name = (null == clazz) ? StrUtil.NULL : clazz.getName(); - } - - @Override - public String getName() { - return this.name; - } - - /** - * 设置自定义的日志显示级别 - * - * @param customLevel 自定义级别 - */ - public static void setLevel(Level customLevel) { - Assert.notNull(customLevel); - currentLevel = customLevel; + super(clazz); } @Override public synchronized void log(String fqcn, Level level, Throwable t, String format, Object... arguments) { - if (!isEnabled(level)) { + if (false == isEnabled(level)) { return; } - System.out.format("\33[%d;2m%s", TIME, DatePattern.NORM_DATETIME_MS_FORMAT.format(new Date())); - switch (level) { - case DEBUG: - System.out.format("\33[%d;2m%-8s", DEBUG, " DEBUG"); - System.out.format("\33[%d;2m%s", DEBUG, " --- "); - break; - case WARN: - System.out.format("\33[%d;2m%-8s", WARN, " WARN"); - System.out.format("\33[%d;2m%s", WARN, " --- "); - break; - case ERROR: - System.out.format("\33[%d;2m%-8s", ERROR, " ERROR"); - System.out.format("\33[%d;2m%s", ERROR, " --- "); - break; - case INFO: - System.out.format("\33[%d;2m%-8s", INFO, " INFO"); - System.out.format("\33[%d;2m%s", INFO, " --- "); - break; - case TRACE: - System.out.format("\33[%d;2m%-8s", TRACE, " TRACE"); - System.out.format("\33[%d;2m%s", TRACE, " --- "); - break; - default: - } - System.out.format("\33[%d;2m%-35s", CLASSNAME, "[" + ClassUtil.getShortClassName(name) + "]"); - System.out.format("\33[%d;2m%s", NONE, " : " + StrUtil.format(format, arguments)); - System.out.format("%s", LINE_SEPARATOR); - System.out.format("\33[%d;2m%s", NONE, ""); - } - @Override - public boolean isDebugEnabled() { - return isEnabled(Level.DEBUG); - } - - @Override - public void debug(String fqcn, Throwable t, String format, Object... arguments) { - log(fqcn, Level.DEBUG, t, format, arguments); - } - - @Override - public boolean isErrorEnabled() { - return isEnabled(Level.ERROR); - } - - @Override - public void error(String fqcn, Throwable t, String format, Object... arguments) { - log(fqcn, Level.ERROR, t, format, arguments); - } - - @Override - public boolean isInfoEnabled() { - return isEnabled(Level.INFO); - } - - @Override - public void info(String fqcn, Throwable t, String format, Object... arguments) { - log(fqcn, Level.INFO, t, format, arguments); - } - - @Override - public boolean isTraceEnabled() { - return isEnabled(Level.TRACE); - } - - @Override - public void trace(String fqcn, Throwable t, String format, Object... arguments) { - log(fqcn, Level.TRACE, t, format, arguments); - } - - @Override - public boolean isWarnEnabled() { - return isEnabled(Level.WARN); - } - - @Override - public void warn(String fqcn, Throwable t, String format, Object... arguments) { - log(fqcn, Level.WARN, t, format, arguments); - } - - @Override - public boolean isEnabled(Level level) { - return currentLevel.compareTo(level) <= 0; + final String template = AnsiEncoder.encode(COLOR_TIME, "[%s]", colorFactory.apply(level), "[%-5s]%s", COLOR_CLASSNAME, "%-30s: ", COLOR_NONE, "%s%n"); + System.out.format(template, DateUtil.now(), level.name(), " - ", ClassUtil.getShortClassName(getName()), StrUtil.format(format, arguments)); } } diff --git a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLogFactory.java b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLogFactory.java old mode 100644 new mode 100755 index 8bcb415ef..bfe514bac --- a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLogFactory.java +++ b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleColorLogFactory.java @@ -4,11 +4,14 @@ import cn.hutool.log.Log; import cn.hutool.log.LogFactory; /** - * @author hongda.li 2022-04-27 09:55 + * 利用System.out.println()打印彩色日志 + * + * @author hongda.li + * @since 5.8.0 */ public class ConsoleColorLogFactory extends LogFactory { - public ConsoleColorLogFactory(){ + public ConsoleColorLogFactory() { super("Hutool Console Color Logging"); } diff --git a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleLog.java b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleLog.java index 4abbe2cfe..71b030971 100644 --- a/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleLog.java +++ b/hutool-log/src/main/java/cn/hutool/log/dialect/console/ConsoleLog.java @@ -141,4 +141,4 @@ public class ConsoleLog extends AbstractLog { public boolean isEnabled(Level level) { return currentLevel.compareTo(level) <= 0; } -} \ No newline at end of file +} diff --git a/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog2.java b/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog2.java index 49de876d4..cebbfb021 100644 --- a/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog2.java +++ b/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog2.java @@ -12,7 +12,7 @@ import org.tinylog.provider.ProviderRegistry; /** * tinylog log.
- * + * * @author Looly * */ @@ -25,12 +25,12 @@ public class TinyLog2 extends AbstractLog { private final int level; private final String name; private static final LoggingProvider provider = ProviderRegistry.getLoggingProvider(); + // ------------------------------------------------------------------------- Constructor + private static final MessageFormatter formatter = new AdvancedMessageFormatter( Configuration.getLocale(), Configuration.isEscapingEnabled() ); - - // ------------------------------------------------------------------------- Constructor public TinyLog2(Class clazz) { this(null == clazz ? StrUtil.NULL : clazz.getName()); } @@ -104,12 +104,12 @@ public class TinyLog2 extends AbstractLog { public void log(String fqcn, cn.hutool.log.level.Level level, Throwable t, String format, Object... arguments) { logIfEnabled(fqcn, toTinyLevel(level), t, format, arguments); } - + @Override public boolean isEnabled(cn.hutool.log.level.Level level) { return this.level <= toTinyLevel(level).ordinal(); } - + /** * 在对应日志级别打开情况下打印日志 * @param fqcn 完全限定类名(Fully Qualified Class Name),用于定位日志位置 @@ -125,10 +125,10 @@ public class TinyLog2 extends AbstractLog { } provider.log(DEPTH, null, level, t, formatter, StrUtil.toString(format), arguments); } - + /** * 将Hutool的Level等级转换为Tinylog的Level等级 - * + * * @param level Hutool的Level等级 * @return Tinylog的Level * @since 4.0.3 @@ -162,7 +162,7 @@ public class TinyLog2 extends AbstractLog { /** * 如果最后一个参数为异常参数,则获取之,否则返回null - * + * * @param arguments 参数 * @return 最后一个异常参数 * @since 4.0.3 diff --git a/hutool-log/src/test/java/cn/hutool/log/test/StaticLogTest.java b/hutool-log/src/test/java/cn/hutool/log/test/StaticLogTest.java old mode 100644 new mode 100755 index 67bcdbbe6..aeac9854f --- a/hutool-log/src/test/java/cn/hutool/log/test/StaticLogTest.java +++ b/hutool-log/src/test/java/cn/hutool/log/test/StaticLogTest.java @@ -1,13 +1,11 @@ package cn.hutool.log.test; import cn.hutool.log.LogFactory; -import cn.hutool.log.dialect.console.ConsoleColorLog; +import cn.hutool.log.StaticLog; import cn.hutool.log.dialect.console.ConsoleColorLogFactory; import cn.hutool.log.dialect.console.ConsoleLogFactory; import org.junit.Test; -import cn.hutool.log.StaticLog; - public class StaticLogTest { @Test public void test() { @@ -24,7 +22,5 @@ public class StaticLogTest { StaticLog.error("This is static {} log", "error"); StaticLog.warn("This is static {} log", "warn"); StaticLog.trace("This is static {} log", "trace"); - ConsoleColorLog.setWarnColor(31); - StaticLog.warn("This is static {} log", "warn"); } }