diff --git a/CHANGELOG.md b/CHANGELOG.md index 84aaef02b..ada53dea3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * 【core 】 修复Dict.of错误(issue#I1UUO5@Gitee) * 【core 】 修复UrlBuilder地址参数问题(issue#I1UWCA@Gitee) * 【core 】 修复StrUtil.toSymbolCase转换问题(issue#1075@Github) +* 【log 】 修复打印null对象显示{msg}异常问题(issue#1084@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java index 28ff651d7..1aad6bea1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/StrUtil.java @@ -2301,13 +2301,13 @@ public class StrUtil { * 转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a
* 转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b
* - * @param template 文本模板,被替换的部分用 {} 表示 + * @param template 文本模板,被替换的部分用 {} 表示,如果模板为null,返回"null" * @param params 参数值 - * @return 格式化后的文本 + * @return 格式化后的文本,如果模板为null,返回"null" */ public static String format(CharSequence template, Object... params) { if (null == template) { - return null; + return NULL; } if (ArrayUtil.isEmpty(params) || isBlank(template)) { return template.toString(); @@ -2337,6 +2337,20 @@ public class StrUtil { * @return 格式化后的文本 */ public static String format(CharSequence template, Map map) { + return format(template, map, true); + } + + /** + * 格式化文本,使用 {varName} 占位
+ * map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue + * + * @param template 文本模板,被替换的部分用 {key} 表示 + * @param map 参数值对 + * @param ignoreNull 是否忽略 {@code null} 值,忽略则 {@code null} 值对应的变量不被替换,否则替换为"" + * @return 格式化后的文本 + * @since 5.4.3 + */ + public static String format(CharSequence template, Map map, boolean ignoreNull) { if (null == template) { return null; } @@ -2348,9 +2362,10 @@ public class StrUtil { String value; for (Entry entry : map.entrySet()) { value = utf8Str(entry.getValue()); - if (null != value) { - template2 = replace(template2, "{" + entry.getKey() + "}", value); + if (null == value && ignoreNull) { + continue; } + template2 = replace(template2, "{" + entry.getKey() + "}", value); } return template2; } @@ -3448,7 +3463,7 @@ public class StrUtil { * @return 位置 */ public static int indexOf(final CharSequence str, char searchChar, int start, int end) { - if(isEmpty(str)){ + if (isEmpty(str)) { return INDEX_NOT_FOUND; } final int len = str.length(); @@ -4363,12 +4378,12 @@ public class StrUtil { * @param strs 多个元素 * @param 元素类型 * @return 第一个非空元素,如果给定的数组为空或者都为空,返回{@code null} - * @since 5.4.1 * @see #isNotEmpty(CharSequence) + * @since 5.4.1 */ @SuppressWarnings("unchecked") public T firstNonEmpty(T... strs) { - return ArrayUtil.firstMatch(StrUtil::isNotEmpty, strs); + return ArrayUtil.firstMatch(StrUtil::isNotEmpty, strs); } /** @@ -4377,8 +4392,8 @@ public class StrUtil { * @param strs 多个元素 * @param 元素类型 * @return 第一个非空元素,如果给定的数组为空或者都为空,返回{@code null} - * @since 5.4.1 * @see #isNotBlank(CharSequence) + * @since 5.4.1 */ @SuppressWarnings("unchecked") public T firstNonBlank(T... strs) { diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index 16c69891d..e64d82b0c 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -18,7 +18,7 @@ - 1.7.26 + 1.7.30 1.3.0-alpha5 1.2.17 2.13.3 @@ -77,5 +77,11 @@ ${jboss-logging.version} true + + org.slf4j + slf4j-simple + ${slf4j.version} + test + 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 4de619a00..5e6ec44e7 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 @@ -120,6 +120,7 @@ public class ConsoleLog extends AbstractLog { return; } + final Dict dict = Dict.create() .set("date", DateUtil.now()) .set("level", level.toString()) diff --git a/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog.java b/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog.java index 945527f43..57da23e8e 100644 --- a/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog.java +++ b/hutool-log/src/main/java/cn/hutool/log/dialect/tinylog/TinyLog.java @@ -116,7 +116,7 @@ public class TinyLog extends AbstractLog { if(null == t){ t = getLastArgumentIfThrowable(arguments); } - LogEntryForwarder.forward(DEPTH, level, t, format, arguments); + LogEntryForwarder.forward(DEPTH, level, t, StrUtil.toString(format), arguments); } /** diff --git a/hutool-log/src/test/java/cn/hutool/log/test/CustomLogTest.java b/hutool-log/src/test/java/cn/hutool/log/test/CustomLogTest.java index b455419e6..f1b46c5d9 100644 --- a/hutool-log/src/test/java/cn/hutool/log/test/CustomLogTest.java +++ b/hutool-log/src/test/java/cn/hutool/log/test/CustomLogTest.java @@ -28,9 +28,18 @@ public class CustomLogTest { LogFactory.setCurrentLogFactory(factory); Log log = LogFactory.get(); - log.info(null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } + + @Test + public void consoleLogNullTest(){ + LogFactory factory = new ConsoleLogFactory(); + LogFactory.setCurrentLogFactory(factory); + Log log = LogFactory.get(); + + log.info(null); + log.info((String)null); + } @Test public void commonsLogTest(){ @@ -39,6 +48,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -49,6 +59,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -61,6 +72,7 @@ public class CustomLogTest { log.debug(null); log.debug("This is custom '{}' log\n{}", factory.getName(), LINE); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -71,6 +83,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -82,6 +95,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -92,6 +106,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } @@ -102,6 +117,7 @@ public class CustomLogTest { Log log = LogFactory.get(); log.info(null); + log.info((String)null); log.info("This is custom '{}' log\n{}", factory.getName(), LINE); } } diff --git a/hutool-log/src/test/java/cn/hutool/log/test/LogTest.java b/hutool-log/src/test/java/cn/hutool/log/test/LogTest.java index 2d4d1fcc8..3a26b50b3 100644 --- a/hutool-log/src/test/java/cn/hutool/log/test/LogTest.java +++ b/hutool-log/src/test/java/cn/hutool/log/test/LogTest.java @@ -37,4 +37,12 @@ public class LogTest { Exception e = new Exception("test Exception"); log.error("我是错误消息", e); } + + @Test + public void logNullTest(){ + final Log log = Log.get(); + log.debug(null); + log.info(null); + log.warn(null); + } }