xChar

适用于文件等大字符串的打印

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;

/**
 * toString 的格式化
 * 1. 不打印null
 * 2. 限定参数的打印长度
 *
 * 可能导致部分main方法中的bean对象toString方法不可用
 *
 * e.g.
 * <code>
 *
 *     @Override
 *     public String toString() {
 *         return ToStringBuilder.reflectionToString(
 *                 this, new MyNoNullStyle().setLimit("file"));
 *     }
 *
 * </code>
 *
 */
@Slf4j
public class MyNoNullStyle extends ToStringStyle implements Serializable {
    private static final long serialVersionUID = 2347542971151578670L;
    /**
     * 不打印的属性
     */
    private String[] exclude;
    /**
     * 限制长度的属性
     */
    private String[] limit;
    /**
     * 限制的长度值
     */
    private int limitLength = 100;

    /**
     * 设置需要忽略打印的属性
     * @param exclude 忽略列表
     * @return this
     */
    public MyNoNullStyle setExclude(String... exclude) {
        this.exclude = exclude;
        return this;
    }

    /**
     * 设置需要限制打印的属性
     * @param limit 限制的属性
     * @return this
     */
    public MyNoNullStyle setLimit(String... limit) {
        this.limit = limit;
        return this;
    }

    /**
     * 设置需要限制打印的长度
     * @param limitLength 限制长度,默认100
     * @return this
     */
    public MyNoNullStyle setLimitLength(int limitLength) {
        this.limitLength = limitLength;
        return this;
    }

    @Override
    public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
        try {
            if (!ObjectUtils.isEmpty(exclude)) {
                for (String es : exclude) {
                    if (fieldName.equalsIgnoreCase(es)) {
                        return;
                    }
                }
            }
            if (value != null) {
                if (!ObjectUtils.isEmpty(limit)) {
                    for (String ls : limit) {
                        if (fieldName.equalsIgnoreCase(ls)) {
                            value = StrUtil.brief(value.toString(), limitLength);
                        }
                    }
                }
                super.append(buffer, fieldName, value, fullDetail);
            }
        } catch (Exception e) {
            log.error("NoNullStyle判断异常[{}]", e);
        }

    }
}

使用:

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, new MyNoNullStyle().setLimit("file"));
    }
Loading comments...