PointUtil.java 3.7 KB
Newer Older
zlt2000's avatar
zlt2000 已提交
1 2
package com.central.log.monitor;

zlt2000's avatar
zlt2000 已提交
3 4 5
import cn.hutool.core.util.ReflectUtil;
import lombok.Getter;
import lombok.Setter;
zlt2000's avatar
zlt2000 已提交
6
import lombok.extern.slf4j.Slf4j;
zlt2000's avatar
zlt2000 已提交
7 8 9 10 11
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
zlt2000's avatar
zlt2000 已提交
12 13 14 15 16

/**
 * 日志埋点工具类
 *
 * @author zlt
zlt2000's avatar
zlt2000 已提交
17 18 19
 * <p>
 * Blog: https://zlt2000.gitee.io
 * Github: https://github.com/zlt2000
zlt2000's avatar
zlt2000 已提交
20 21 22
 */
@Slf4j
public class PointUtil {
zlt2000's avatar
zlt2000 已提交
23
    private static final String MSG_PATTERN = "{}|{}|{}";
zlt2000's avatar
zlt2000 已提交
24 25 26 27
    private static final String PROPERTIES_SPLIT = "&";
    private static final String PROPERTIES_VALUE_SPLIT = "=";

    private final PointEntry pointEntry;
zlt2000's avatar
zlt2000 已提交
28

29
    private PointUtil() {
zlt2000's avatar
zlt2000 已提交
30 31 32 33 34 35 36 37 38
        pointEntry = new PointEntry();
    }

    @Setter
    @Getter
    private class PointEntry {
        String id;
        String type;
        Object properties;
39 40
    }

zlt2000's avatar
zlt2000 已提交
41 42 43 44 45 46 47 48 49 50
    /**
     * 格式为:{时间}|{来源}|{对象id}|{类型}|{对象属性(以&分割)}
     * 例子1:2016-07-27 23:37:23|business-center|1|user-login|ip=xxx.xxx.xx&userName=张三&userType=后台管理员
     * 例子2:2016-07-27 23:37:23|file-center|c0a895e114526786450161001d1ed9|file-upload|fileName=xxx&filePath=xxx
     *
     * @param id      对象id
     * @param type    类型
     * @param message 对象属性
     */
    public static void info(String id, String type, String message) {
zlt2000's avatar
zlt2000 已提交
51
        log.info(MSG_PATTERN, id, type, message);
zlt2000's avatar
zlt2000 已提交
52
    }
53 54

    public static void debug(String id, String type, String message) {
zlt2000's avatar
zlt2000 已提交
55
        log.debug(MSG_PATTERN, id, type, message);
56
    }
zlt2000's avatar
zlt2000 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

    public static PointUtil builder() {
        return new PointUtil();
    }

    /**
     * @param businessId 业务id/对象id
     */
    public PointUtil id(Object businessId) {
        this.pointEntry.setId(String.valueOf(businessId));
        return this;
    }

    /**
     * @param type 类型
     */
    public PointUtil type(String type) {
        this.pointEntry.setType(type);
        return this;
    }

    /**
     * @param properties 对象属性
     */
    public PointUtil properties(Object properties) {
        this.pointEntry.setProperties(properties);
        return this;
    }

    private String getPropertiesStr() {
        Object properties = this.pointEntry.getProperties();
        StringBuilder result = new StringBuilder();
        if (!ObjectUtils.isEmpty(properties)) {
            //解析map
            if (properties instanceof Map) {
                Map proMap = (Map)properties;
                Iterator<Map.Entry> ite = proMap.entrySet().iterator();
                while (ite.hasNext()) {
                    Map.Entry entry = ite.next();
                    if (result.length() > 0) {
                        result.append(PROPERTIES_SPLIT);
                    }
                    result.append(entry.getKey()).append(PROPERTIES_VALUE_SPLIT).append(entry.getValue());
                }
            } else {//解析对象
                Field[] allFields = ReflectUtil.getFields(properties.getClass());
                for (Field field : allFields) {
                    String fieldName = field.getName();
                    Object fieldValue = ReflectUtil.getFieldValue(properties, field);
                    if (result.length() > 0) {
                        result.append(PROPERTIES_SPLIT);
                    }
                    result.append(fieldName).append(PROPERTIES_VALUE_SPLIT).append(fieldValue);
                }
            }
        }
        return result.toString();
    }

    public void build() {
        PointUtil.debug(this.pointEntry.getId(), this.pointEntry.getType(), this.getPropertiesStr());
    }
zlt2000's avatar
zlt2000 已提交
119
}