泛型日志工具类(LogUtil)

本文介绍了一种泛型日志工具类的设计与实现,该工具类能对比两个对象的属性变化,并以JSONArray格式输出差异,适用于记录数据变更日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于日志工具类

泛型日志工具类(LogUtil)

主要记录库表某条数据发生变化时,变化前后进行对比,进行打印

设计一个工具类,通过传入两个对象,获得属性值变化前后的值,同时以JSONArray的方式返回。

工具类代码

package com.fyhf.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : qisw
 * @desc : 存日志的辅助工具类  通过参入两个对象,能够以jsonarray的方式输出属性不同的值
 *          oldBean 表示修改之前的对象   newBean 表示修改之后的对象
 * @date : 2019-11-21 17:41
 */
public class LogUtil<T> {

    public JSONArray compareObject(Object oldBean, Object newBean) {
        T pojo1 = (T) oldBean;
        T pojo2 = (T) newBean;
        JSONArray array = new JSONArray();
        try {
            Class clazz = pojo1.getClass();
            Field[] oldFields = pojo1.getClass().getDeclaredFields();
            Field[] newFields = pojo2.getClass().getDeclaredFields();

            for (Field oldFileld: oldFields){
                oldFileld.setAccessible(true);//设置属性可读
                String old_name = oldFileld.getName();//获取属性名称
                if(StringUtils.equalsIgnoreCase("id",old_name)){ //忽略id的比较
                    continue;
                }
                for(Field newFileld: newFields){
                    newFileld.setAccessible(true);
                    String new_name = newFileld.getName();//获取属性名称
                    if(StringUtils.equals(old_name,new_name)){
                        Object o1 = oldFileld.get(pojo1);//当前对象对应的属性值
                        Object o2 = newFileld.get(pojo2);//上一个对应的属性值
                        if (o1 == null || o2 == null) {
                            if(o1 == null && o2 == null){
                                continue;
                            }
                            JSONObject job = new JSONObject();
                            JSONObject job1 = new JSONObject();
                            job.put("before",o1);
                            job.put("after",o2);
                            job1.put(new_name,job);
                            array.add(job1);
                            continue;
                        }
                        //两个对象的属性值不一样 表示这个字段被修改过
                        if(!o1.toString().equals(o2.toString())){ 
                            JSONObject job = new JSONObject();
                            JSONObject job1 = new JSONObject();
                            job.put("before",o1);
                            job.put("after",o2);
                            job1.put(new_name,job);
                            array.add(job1);
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }
}

实体类代码

package com.example.demo;

/**
 * @author : qisw
 * @desc :
 * @date : 2019-11-16 17:45
 */
public class Host {
    private String ip;
    private int port;
    private String name;
    private String cTime;

    public String getcTime() {
        return cTime;
    }

    public void setcTime(String cTime) {
        this.cTime = cTime;
    }

    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Host(String ip, int port, String name, String cTime) {
        this.ip = ip;
        this.port = port;
        this.name = name;
        this.cTime = cTime;
    }
}

测试类代码

@Test
	public void testLogUtil(){
		Host host1 = new Host("135",8080,"host1","2018-03-01");
		Host host2 = new Host("136",8081,"host2","2018-05-01");
		LogUtil<Host> logUtil = new LogUtil<>();
		JSONArray array = logUtil.compareObject(host1, host2);
		System.out.println(array);
	}

测试结果图片

测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值