背景说明:
编码规范要求日志打印控制日志打印的量,控制对象打印以及list打印,单个对象打印相对好处理,list对象打印处理相对麻烦
优化说明:
通过传入参数,选取list中需要打印的内容,组装之后返回,然后统一打印处理啊。
对应代码:
public class FunctionalStudy {
private final static Logger logger = LoggerFactory.getLogger(FunctionalStudy.class);
public static void main(String[] args) {
Apple apple1 = new Apple("red", 100);
Apple apple2 = new Apple("green", 110);
Apple apple3 = new Apple("yellow", 120);
List<Apple> appleList = Lists.newArrayList(apple1, apple2, apple3);
logger.info("apple print=[{}]", getListString(appleList, Apple::getColor, Apple::getWeight));
}
/**
* list内容打印调整,传入list--返回[[column1=["11","22","33"], column2=[1,2,3]]]
* @param data--目标list
* @param get--需要打印字段的get方法
* @return
* @param <T>
* @param <R>
*/
public static <T, R> List<String> getListString(List<T> data, SFunction<T, R> ...get){
List<String> str = new ArrayList<>();
try {
for (int i = 0; i < get.length; i++) {
List<R> targetList = data.stream().map(get[i]).collect(Collectors.toList());
// mybatis-plus-core 3.4.3
LambdaMeta meta = LambdaUtils.extract(get[i]);
String methodName = meta.getImplMethodName();
if(StringUtils.isNoneBlank(methodName) && methodName.length() > 3){
str.add(methodName.substring(3) + "=" + JSONObject.toJSONString(targetList));
} else {
str.add(methodName + "=" + JSONObject.toJSONString(targetList));
}
}
} catch (Exception e) {
System.out.println("日志打印");
}
return str;
}
}
还可以优化成对象形式打印,这里只是本人记录内容,不喜勿喷