fix log bug

This commit is contained in:
Looly 2020-09-16 13:52:26 +08:00
parent 667dbc2be7
commit f9f73e4c02
7 changed files with 59 additions and 12 deletions

View File

@ -21,6 +21,7 @@
* 【core 】 修复Dict.of错误issue#I1UUO5@Gitee * 【core 】 修复Dict.of错误issue#I1UUO5@Gitee
* 【core 】 修复UrlBuilder地址参数问题issue#I1UWCA@Gitee * 【core 】 修复UrlBuilder地址参数问题issue#I1UWCA@Gitee
* 【core 】 修复StrUtil.toSymbolCase转换问题issue#1075@Github * 【core 】 修复StrUtil.toSymbolCase转换问题issue#1075@Github
* 【log 】 修复打印null对象显示{msg}异常问题issue#1084@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -2301,13 +2301,13 @@ public class StrUtil {
* 转义{} format("this is \\{} for {}", "a", "b") = this is \{} for a<br> * 转义{} format("this is \\{} for {}", "a", "b") = this is \{} for a<br>
* 转义\ format("this is \\\\{} for {}", "a", "b") = this is \a for b<br> * 转义\ format("this is \\\\{} for {}", "a", "b") = this is \a for b<br>
* *
* @param template 文本模板被替换的部分用 {} 表示 * @param template 文本模板被替换的部分用 {} 表示如果模板为null返回"null"
* @param params 参数值 * @param params 参数值
* @return 格式化后的文本 * @return 格式化后的文本如果模板为null返回"null"
*/ */
public static String format(CharSequence template, Object... params) { public static String format(CharSequence template, Object... params) {
if (null == template) { if (null == template) {
return null; return NULL;
} }
if (ArrayUtil.isEmpty(params) || isBlank(template)) { if (ArrayUtil.isEmpty(params) || isBlank(template)) {
return template.toString(); return template.toString();
@ -2337,6 +2337,20 @@ public class StrUtil {
* @return 格式化后的文本 * @return 格式化后的文本
*/ */
public static String format(CharSequence template, Map<?, ?> map) { public static String format(CharSequence template, Map<?, ?> map) {
return format(template, map, true);
}
/**
* 格式化文本使用 {varName} 占位<br>
* 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) { if (null == template) {
return null; return null;
} }
@ -2348,9 +2362,10 @@ public class StrUtil {
String value; String value;
for (Entry<?, ?> entry : map.entrySet()) { for (Entry<?, ?> entry : map.entrySet()) {
value = utf8Str(entry.getValue()); value = utf8Str(entry.getValue());
if (null != value) { if (null == value && ignoreNull) {
template2 = replace(template2, "{" + entry.getKey() + "}", value); continue;
} }
template2 = replace(template2, "{" + entry.getKey() + "}", value);
} }
return template2; return template2;
} }
@ -4363,8 +4378,8 @@ public class StrUtil {
* @param strs 多个元素 * @param strs 多个元素
* @param <T> 元素类型 * @param <T> 元素类型
* @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null} * @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null}
* @since 5.4.1
* @see #isNotEmpty(CharSequence) * @see #isNotEmpty(CharSequence)
* @since 5.4.1
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends CharSequence> T firstNonEmpty(T... strs) { public <T extends CharSequence> T firstNonEmpty(T... strs) {
@ -4377,8 +4392,8 @@ public class StrUtil {
* @param strs 多个元素 * @param strs 多个元素
* @param <T> 元素类型 * @param <T> 元素类型
* @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null} * @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null}
* @since 5.4.1
* @see #isNotBlank(CharSequence) * @see #isNotBlank(CharSequence)
* @since 5.4.1
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends CharSequence> T firstNonBlank(T... strs) { public <T extends CharSequence> T firstNonBlank(T... strs) {

View File

@ -18,7 +18,7 @@
<properties> <properties>
<!-- versions --> <!-- versions -->
<slf4j.version>1.7.26</slf4j.version> <slf4j.version>1.7.30</slf4j.version>
<logback.version>1.3.0-alpha5</logback.version> <logback.version>1.3.0-alpha5</logback.version>
<log4j.version>1.2.17</log4j.version> <log4j.version>1.2.17</log4j.version>
<log4j2.version>2.13.3</log4j2.version> <log4j2.version>2.13.3</log4j2.version>
@ -77,5 +77,11 @@
<version>${jboss-logging.version}</version> <version>${jboss-logging.version}</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -120,6 +120,7 @@ public class ConsoleLog extends AbstractLog {
return; return;
} }
final Dict dict = Dict.create() final Dict dict = Dict.create()
.set("date", DateUtil.now()) .set("date", DateUtil.now())
.set("level", level.toString()) .set("level", level.toString())

View File

@ -116,7 +116,7 @@ public class TinyLog extends AbstractLog {
if(null == t){ if(null == t){
t = getLastArgumentIfThrowable(arguments); t = getLastArgumentIfThrowable(arguments);
} }
LogEntryForwarder.forward(DEPTH, level, t, format, arguments); LogEntryForwarder.forward(DEPTH, level, t, StrUtil.toString(format), arguments);
} }
/** /**

View File

@ -28,10 +28,19 @@ public class CustomLogTest {
LogFactory.setCurrentLogFactory(factory); LogFactory.setCurrentLogFactory(factory);
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); 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 @Test
public void commonsLogTest(){ public void commonsLogTest(){
LogFactory factory = new ApacheCommonsLogFactory(); LogFactory factory = new ApacheCommonsLogFactory();
@ -39,6 +48,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -49,6 +59,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -61,6 +72,7 @@ public class CustomLogTest {
log.debug(null); log.debug(null);
log.debug("This is custom '{}' log\n{}", factory.getName(), LINE); log.debug("This is custom '{}' log\n{}", factory.getName(), LINE);
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -71,6 +83,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -82,6 +95,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -92,6 +106,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
@ -102,6 +117,7 @@ public class CustomLogTest {
Log log = LogFactory.get(); Log log = LogFactory.get();
log.info(null); log.info(null);
log.info((String)null);
log.info("This is custom '{}' log\n{}", factory.getName(), LINE); log.info("This is custom '{}' log\n{}", factory.getName(), LINE);
} }
} }

View File

@ -37,4 +37,12 @@ public class LogTest {
Exception e = new Exception("test Exception"); Exception e = new Exception("test Exception");
log.error("我是错误消息", e); log.error("我是错误消息", e);
} }
@Test
public void logNullTest(){
final Log log = Log.get();
log.debug(null);
log.info(null);
log.warn(null);
}
} }