Commit 0aaad4d0 authored by yanzg's avatar yanzg

修复异常提醒,从而正确的跟踪异常信息

parent b664f258
......@@ -23,6 +23,17 @@ public class CollectionString {
* @return 表格字符串
*/
public static <T> String getCollectionString(String tag, Collection<T> rowList) {
return getCollectionString(tag, rowList, null);
}
/**
* 集合转换为表格字符串
*
* @param rowList 需要转换的集合
* @param <T> 集合类型
* @return 表格字符串
*/
public static <T> String getCollectionString(String tag, Collection<T> rowList, Handle<T> handle) {
// 每个字段的长度
Map<String, Integer> mapFieldCount = getValueLength(rowList);
// 删除为0的长度,并将列名长度写入
......@@ -36,7 +47,11 @@ public class CollectionString {
mapFieldCount.forEach((name, len) -> {
int nameLen = length(name);
appendChar(sbResult, EMPTY_SIZE, EMPTY);
sbResult.append(name);
if (handle != null) {
sbResult.append(handle.columnName(sbResult, name));
} else {
sbResult.append(name);
}
appendChar(sbResult, len - nameLen + EMPTY_SIZE, EMPTY);
sbResult.append(SPLIT);
});
......@@ -44,15 +59,25 @@ public class CollectionString {
// 遍历每一行
rowList.forEach(row -> {
sbResult.append(System.lineSeparator());
if (handle != null) {
handle.start(sbResult, row);
}
sbResult.append(SPLIT);
mapFieldCount.forEach((name, len) -> {
appendChar(sbResult, EMPTY_SIZE, EMPTY);
// 获取字符串
String value = StringHelper.getFirst(ObjectHelper.getString(row, name));
sbResult.append(value);
if (handle != null) {
sbResult.append(handle.columnValue(sbResult, row, name, value));
} else {
sbResult.append(value);
}
appendChar(sbResult, len - length(value) + EMPTY_SIZE, EMPTY);
sbResult.append(SPLIT);
});
if (handle != null) {
handle.end(sbResult, row);
}
});
appendEmptyRow(sbResult, mapFieldCount);
return sbResult.toString();
......@@ -83,7 +108,7 @@ public class CollectionString {
/**
* 删除为0的长度,并将列名长度写入
*
* @param mapFieldCount
* @param mapFieldCount 初始化没列的长度
*/
private static void initColumnNameLength(Map<String, Integer> mapFieldCount) {
// 添加到另外一个集合,防止遍历列名通知执行删除时出错
......@@ -102,8 +127,8 @@ public class CollectionString {
/**
* 添加空行
*
* @param sbResult
* @param mapFieldCount
* @param sbResult 字符串
* @param mapFieldCount 字符串值
*/
private static void appendEmptyRow(StringBuilder sbResult, Map<String, Integer> mapFieldCount) {
if (sbResult.length() > 0) {
......@@ -154,4 +179,43 @@ public class CollectionString {
}
return chLength * 5 / 3 + enLength;
}
public interface Handle<T> {
/**
* 处理一行的开始数据
*
* @param sb 字符串
* @param row 行数据
*/
void start(StringBuilder sb, T row);
/**
* 处理一行的结束数据
*
* @param sb 字符串
* @param row 行数据
*/
void end(StringBuilder sb, T row);
/**
* 处理一行中的某一列
*
* @param sb 字符串
* @param name 列名
* @return 返回值
*/
String columnName(StringBuilder sb, String name);
/**
* 处理一行中的某一列
*
* @param sb 字符串
* @param row 行数据
* @param name 列名
* @param value 列值
* @return 返回值
*/
String columnValue(StringBuilder sb, T row, String name, String value);
}
}
package com.yanzuoguang.util.base;
public class ColorUtils {
// ANSI 颜色代码
public enum Color {
RESET("\033[0m"),
BLACK("\033[30m"),
RED("\033[31m"),
GREEN("\033[32m"),
YELLOW("\033[33m"),
BLUE("\033[34m"),
MAGENTA("\033[35m"),
CYAN("\033[36m"),
WHITE("\033[37m");
private final String code;
Color(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
// 带颜色输出文本
public static String colorize(String text, Color color) {
return color + text + Color.RESET;
}
}
package base;
import com.yanzuoguang.util.base.ColorUtils;
import org.junit.Test;
import java.util.function.Consumer;
......@@ -13,4 +14,10 @@ public class TestDemo {
Consumer<String> consumer = demo::setName;
System.out.println(consumer.toString());
}
@Test
public void testRow(){
System.out.println(ColorUtils.colorize("颜佐光", ColorUtils.Color.RED)); ;
System.out.println(ColorUtils.colorize("颜佐光", ColorUtils.Color.GREEN)); ;
}
}
......@@ -2,6 +2,7 @@ package helper;
import base.DemoVo;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.base.ColorUtils;
import com.yanzuoguang.util.helper.StringHelper;
import org.junit.Test;
......@@ -22,6 +23,44 @@ public class TestCollectionString {
System.out.println(CollectionString.getCollectionString(StringHelper.EMPTY, mapList));
}
@Test
public void testCh1() {
List<DemoVo> mapList = new ArrayList<>(Arrays.asList(
new DemoVo("1", "颜佐光"),
new DemoVo("10000", "成吉思汗"),
new DemoVo("10001", "毛泽东")
));
System.out.println(CollectionString.getCollectionString("人名:", mapList, new CollectionString.Handle<DemoVo>() {
@Override
public void start(StringBuilder sb, DemoVo row) {
if (StringHelper.compare(row.getId(), "1")) {
sb.append(ColorUtils.Color.RED);
}
}
@Override
public void end(StringBuilder sb, DemoVo row) {
if (StringHelper.compare(row.getId(), "1")) {
sb.append(ColorUtils.Color.RESET);
}
}
@Override
public String columnName(StringBuilder sb, String name) {
return name;
}
@Override
public String columnValue(StringBuilder sb, DemoVo row, String name, String value) {
if (StringHelper.compare(value, "毛泽东")) {
return ColorUtils.colorize(value, ColorUtils.Color.GREEN);
} else {
return value;
}
}
}));
}
@Test
public void testEn() {
......@@ -33,6 +72,5 @@ public class TestCollectionString {
System.out.println(CollectionString.getCollectionString(StringHelper.EMPTY, mapList));
System.out.println(CollectionString.getCollectionString("人名:", mapList));
}
}
package com.yanzuoguang.log;
import com.yanzuoguang.util.base.CollectionString;
import com.yanzuoguang.util.base.ColorUtils;
import org.springframework.stereotype.Component;
import java.util.List;
......@@ -29,7 +30,39 @@ public class LogFeignDefault implements LogFeignBase {
public void notifyTodayCount(LogCountResult todayResult) {
// 打印日志
String tag = todayResult.getTodayTime() + "执行次数:";
String collectionString = CollectionString.getCollectionString(tag, todayResult.getList());
String collectionString = CollectionString.getCollectionString(tag, todayResult.getList(), new CollectionString.Handle<LogUrlCountVo>() {
@Override
public void start(StringBuilder sb, LogUrlCountVo row) {
if (row.getLevel() == -1 || row.getLevel() >= 5000) {
sb.append(ColorUtils.Color.RED);
} else if (row.getLevel() >= 1000) {
sb.append(ColorUtils.Color.YELLOW);
} else if (row.getLevel() < 100) {
sb.append(ColorUtils.Color.GREEN);
}
}
@Override
public void end(StringBuilder sb, LogUrlCountVo row) {
if (row.getLevel() == -1 || row.getLevel() >= 5000) {
sb.append(ColorUtils.Color.RESET);
} else if (row.getLevel() >= 1000) {
sb.append(ColorUtils.Color.RESET);
} else if (row.getLevel() < 100) {
sb.append(ColorUtils.Color.RESET);
}
}
@Override
public String columnName(StringBuilder sb, String name) {
return name;
}
@Override
public String columnValue(StringBuilder sb, LogUrlCountVo row, String name, String value) {
return value;
}
});
System.out.println(collectionString);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment