mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
enhance Console.log
This commit is contained in:
@@ -3,11 +3,13 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.4.3 (2020-09-10)
|
# 5.4.3 (2020-09-11)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 使用静态的of方法来new对象(pr#177@Gitee)
|
* 【core 】 使用静态的of方法来new对象(pr#177@Gitee)
|
||||||
* 【setting】 Setting增加store无参方法(issue#1072@Github)
|
* 【setting】 Setting增加store无参方法(issue#1072@Github)
|
||||||
|
* 【setting】 StatementUtil增加null缓存(pr#1076@Github)
|
||||||
|
* 【core 】 扩充Console功能,支持可变参数(issue#1077@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package cn.hutool.core.lang;
|
package cn.hutool.core.lang;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.CharUtil;
|
import cn.hutool.core.util.CharUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
@@ -13,12 +14,14 @@ import static java.lang.System.out;
|
|||||||
* 此类主要针对{@link System#out} 和 {@link System#err} 做封装。
|
* 此类主要针对{@link System#out} 和 {@link System#err} 做封装。
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Console {
|
public class Console {
|
||||||
|
|
||||||
|
private static final String TEMPLATE_VAR = "{}";
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------- Log
|
// --------------------------------------------------------------------------------- Log
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同 System.out.println()方法,打印控制台日志
|
* 同 System.out.println()方法,打印控制台日志
|
||||||
*/
|
*/
|
||||||
@@ -34,13 +37,70 @@ public class Console {
|
|||||||
*/
|
*/
|
||||||
public static void log(Object obj) {
|
public static void log(Object obj) {
|
||||||
if (obj instanceof Throwable) {
|
if (obj instanceof Throwable) {
|
||||||
Throwable e = (Throwable) obj;
|
final Throwable e = (Throwable) obj;
|
||||||
log(e, e.getMessage());
|
log(e, e.getMessage());
|
||||||
} else {
|
} else {
|
||||||
log("{}", obj);
|
log(TEMPLATE_VAR, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志<br>
|
||||||
|
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈
|
||||||
|
*
|
||||||
|
* @param otherObjs 要打印的对象
|
||||||
|
* @since 5.4.3
|
||||||
|
*/
|
||||||
|
public static void log(Object obj1, Object... otherObjs) {
|
||||||
|
if(ArrayUtil.isEmpty(otherObjs)){
|
||||||
|
log(obj1);
|
||||||
|
} else{
|
||||||
|
log(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志<br>
|
||||||
|
* 当传入template无"{}"时,被认为非模板,直接打印多个参数以空格分隔
|
||||||
|
*
|
||||||
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
|
* @param values 值
|
||||||
|
*/
|
||||||
|
public static void log(String template, Object... values) {
|
||||||
|
if (ArrayUtil.isEmpty(values) || StrUtil.contains(template, TEMPLATE_VAR)) {
|
||||||
|
logInternal(template, values);
|
||||||
|
} else {
|
||||||
|
logInternal(buildTemplateSplitBySpace(values.length + 1), ArrayUtil.insert(values, 0, template));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志
|
||||||
|
*
|
||||||
|
* @param t 异常对象
|
||||||
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
|
* @param values 值
|
||||||
|
*/
|
||||||
|
public static void log(Throwable t, String template, Object... values) {
|
||||||
|
out.println(StrUtil.format(template, values));
|
||||||
|
if (null != t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志
|
||||||
|
*
|
||||||
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
|
* @param values 值
|
||||||
|
* @since 5.4.3
|
||||||
|
*/
|
||||||
|
private static void logInternal(String template, Object... values){
|
||||||
|
log(null, template, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------------- print
|
||||||
/**
|
/**
|
||||||
* 同 System.out.print()方法,打印控制台日志
|
* 同 System.out.print()方法,打印控制台日志
|
||||||
*
|
*
|
||||||
@@ -48,7 +108,37 @@ public class Console {
|
|||||||
* @since 3.3.1
|
* @since 3.3.1
|
||||||
*/
|
*/
|
||||||
public static void print(Object obj) {
|
public static void print(Object obj) {
|
||||||
print("{}", obj);
|
print(TEMPLATE_VAR, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志<br>
|
||||||
|
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈
|
||||||
|
*
|
||||||
|
* @param otherObjs 要打印的对象
|
||||||
|
* @since 5.4.3
|
||||||
|
*/
|
||||||
|
public static void print(Object obj1, Object... otherObjs) {
|
||||||
|
if(ArrayUtil.isEmpty(otherObjs)){
|
||||||
|
print(obj1);
|
||||||
|
} else{
|
||||||
|
print(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.print()方法,打印控制台日志
|
||||||
|
*
|
||||||
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
|
* @param values 值
|
||||||
|
* @since 3.3.1
|
||||||
|
*/
|
||||||
|
public static void print(String template, Object... values) {
|
||||||
|
if (ArrayUtil.isEmpty(values) || StrUtil.contains(template, TEMPLATE_VAR)) {
|
||||||
|
printInternal(template, values);
|
||||||
|
} else {
|
||||||
|
printInternal(buildTemplateSplitBySpace(values.length + 1), ArrayUtil.insert(values, 0, template));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,38 +170,14 @@ public class Console {
|
|||||||
*
|
*
|
||||||
* @param template 文本模板,被替换的部分用 {} 表示
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
* @param values 值
|
* @param values 值
|
||||||
|
* @since 5.4.3
|
||||||
*/
|
*/
|
||||||
public static void log(String template, Object... values) {
|
private static void printInternal(String template, Object... values){
|
||||||
log(null, template, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 同 System.out.print()方法,打印控制台日志
|
|
||||||
*
|
|
||||||
* @param template 文本模板,被替换的部分用 {} 表示
|
|
||||||
* @param values 值
|
|
||||||
* @since 3.3.1
|
|
||||||
*/
|
|
||||||
public static void print(String template, Object... values) {
|
|
||||||
out.print(StrUtil.format(template, values));
|
out.print(StrUtil.format(template, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 同 System.out.println()方法,打印控制台日志
|
|
||||||
*
|
|
||||||
* @param t 异常对象
|
|
||||||
* @param template 文本模板,被替换的部分用 {} 表示
|
|
||||||
* @param values 值
|
|
||||||
*/
|
|
||||||
public static void log(Throwable t, String template, Object... values) {
|
|
||||||
out.println(StrUtil.format(template, values));
|
|
||||||
if (null != t) {
|
|
||||||
t.printStackTrace();
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------- Error
|
// --------------------------------------------------------------------------------- Error
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同 System.err.println()方法,打印控制台日志
|
* 同 System.err.println()方法,打印控制台日志
|
||||||
*/
|
*/
|
||||||
@@ -129,7 +195,22 @@ public class Console {
|
|||||||
Throwable e = (Throwable) obj;
|
Throwable e = (Throwable) obj;
|
||||||
error(e, e.getMessage());
|
error(e, e.getMessage());
|
||||||
} else {
|
} else {
|
||||||
error("{}", obj);
|
error(TEMPLATE_VAR, obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.out.println()方法,打印控制台日志<br>
|
||||||
|
* 如果传入打印对象为{@link Throwable}对象,那么同时打印堆栈
|
||||||
|
*
|
||||||
|
* @param otherObjs 要打印的对象
|
||||||
|
* @since 5.4.3
|
||||||
|
*/
|
||||||
|
public static void error(Object obj1, Object... otherObjs) {
|
||||||
|
if(ArrayUtil.isEmpty(otherObjs)){
|
||||||
|
error(obj1);
|
||||||
|
} else{
|
||||||
|
error(buildTemplateSplitBySpace(otherObjs.length + 1), ArrayUtil.insert(otherObjs, 0, obj1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +221,11 @@ public class Console {
|
|||||||
* @param values 值
|
* @param values 值
|
||||||
*/
|
*/
|
||||||
public static void error(String template, Object... values) {
|
public static void error(String template, Object... values) {
|
||||||
error(null, template, values);
|
if (ArrayUtil.isEmpty(values) || StrUtil.contains(template, TEMPLATE_VAR)) {
|
||||||
|
errorInternal(template, values);
|
||||||
|
} else {
|
||||||
|
errorInternal(buildTemplateSplitBySpace(values.length + 1), ArrayUtil.insert(values, 0, template));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,7 +243,18 @@ public class Console {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同 System.err.println()方法,打印控制台日志
|
||||||
|
*
|
||||||
|
* @param template 文本模板,被替换的部分用 {} 表示
|
||||||
|
* @param values 值
|
||||||
|
*/
|
||||||
|
private static void errorInternal(String template, Object... values) {
|
||||||
|
error(null, template, values);
|
||||||
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------- in
|
// --------------------------------------------------------------------------------- in
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建从控制台读取内容的{@link Scanner}
|
* 创建从控制台读取内容的{@link Scanner}
|
||||||
*
|
*
|
||||||
@@ -180,6 +276,7 @@ public class Console {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------------- console lineNumber
|
// --------------------------------------------------------------------------------- console lineNumber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回当前位置+行号 (不支持Lambda、内部类、递归内使用)
|
* 返回当前位置+行号 (不支持Lambda、内部类、递归内使用)
|
||||||
*
|
*
|
||||||
@@ -206,4 +303,14 @@ public class Console {
|
|||||||
return new Throwable().getStackTrace()[1].getLineNumber();
|
return new Throwable().getStackTrace()[1].getLineNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建空格分隔的模板,类似于"{} {} {} {}"
|
||||||
|
*
|
||||||
|
* @param count 变量数量
|
||||||
|
* @return 模板
|
||||||
|
*/
|
||||||
|
private static String buildTemplateSplitBySpace(int count){
|
||||||
|
return StrUtil.repeatAndJoin(TEMPLATE_VAR, count, StrUtil.SPACE);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -123,7 +123,7 @@ public class RandomUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得随机数[0, 2^32)
|
* 获得随机数int值
|
||||||
*
|
*
|
||||||
* @return 随机数
|
* @return 随机数
|
||||||
*/
|
*/
|
||||||
|
@@ -22,6 +22,12 @@ public class ConsoleTest {
|
|||||||
Console.log("This is Console log for {}.", "test");
|
Console.log("This is Console log for {}.", "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void logTest2(){
|
||||||
|
Console.log("a", "b", "c");
|
||||||
|
Console.log((Object) "a", "b", "c");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void printTest(){
|
public void printTest(){
|
||||||
String[] a = {"abc", "bcd", "def"};
|
String[] a = {"abc", "bcd", "def"};
|
||||||
@@ -30,6 +36,12 @@ public class ConsoleTest {
|
|||||||
Console.log("This is Console print for {}.", "test");
|
Console.log("This is Console print for {}.", "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void printTest2(){
|
||||||
|
Console.print("a", "b", "c");
|
||||||
|
Console.print((Object) "a", "b", "c");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void errorTest(){
|
public void errorTest(){
|
||||||
Console.error();
|
Console.error();
|
||||||
@@ -40,6 +52,12 @@ public class ConsoleTest {
|
|||||||
Console.error("This is Console error for {}.", "test");
|
Console.error("This is Console error for {}.", "test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void errorTest2(){
|
||||||
|
Console.error("a", "b", "c");
|
||||||
|
Console.error((Object) "a", "b", "c");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void inputTest() {
|
public void inputTest() {
|
||||||
|
@@ -163,9 +163,9 @@ public class StatementUtil {
|
|||||||
sql = sql.trim();
|
sql = sql.trim();
|
||||||
SqlLog.INSTANCE.log(sql, paramsBatch);
|
SqlLog.INSTANCE.log(sql, paramsBatch);
|
||||||
PreparedStatement ps = conn.prepareStatement(sql);
|
PreparedStatement ps = conn.prepareStatement(sql);
|
||||||
Map<Integer, Integer> nullTypeMap = new HashMap<>();
|
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
|
||||||
for (Object[] params : paramsBatch) {
|
for (Object[] params : paramsBatch) {
|
||||||
StatementUtil.fillParams(ps, params, nullTypeMap);
|
fillParams(ps, new ArrayIter<>(params), nullTypeMap);
|
||||||
ps.addBatch();
|
ps.addBatch();
|
||||||
}
|
}
|
||||||
return ps;
|
return ps;
|
||||||
@@ -191,7 +191,7 @@ public class StatementUtil {
|
|||||||
//null参数的类型缓存,避免循环中重复获取类型
|
//null参数的类型缓存,避免循环中重复获取类型
|
||||||
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
|
final Map<Integer, Integer> nullTypeMap = new HashMap<>();
|
||||||
for (Entity entity : entities) {
|
for (Entity entity : entities) {
|
||||||
StatementUtil.fillParams(ps, CollectionUtil.valuesOfKeys(entity, fields), nullTypeMap);
|
fillParams(ps, CollectionUtil.valuesOfKeys(entity, fields), nullTypeMap);
|
||||||
ps.addBatch();
|
ps.addBatch();
|
||||||
}
|
}
|
||||||
return ps;
|
return ps;
|
||||||
|
Reference in New Issue
Block a user