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 】 修复UrlBuilder地址参数问题issue#I1UWCA@Gitee
* 【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 \a for b<br>
*
* @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} 占位<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) {
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,8 +4378,8 @@ public class StrUtil {
* @param strs 多个元素
* @param <T> 元素类型
* @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null}
* @since 5.4.1
* @see #isNotEmpty(CharSequence)
* @since 5.4.1
*/
@SuppressWarnings("unchecked")
public <T extends CharSequence> T firstNonEmpty(T... strs) {
@ -4377,8 +4392,8 @@ public class StrUtil {
* @param strs 多个元素
* @param <T> 元素类型
* @return 第一个非空元素如果给定的数组为空或者都为空返回{@code null}
* @since 5.4.1
* @see #isNotBlank(CharSequence)
* @since 5.4.1
*/
@SuppressWarnings("unchecked")
public <T extends CharSequence> T firstNonBlank(T... strs) {

View File

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

View File

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

View File

@ -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);
}
/**

View File

@ -28,10 +28,19 @@ 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(){
LogFactory factory = new ApacheCommonsLogFactory();
@ -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);
}
}

View File

@ -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);
}
}