日志工具类之“根据标记的注解进行指定的字段日志记录-在展示方式上会美观一些”

一、使用方法

在添加、编辑等操作功能时可以使用该方案,在需要记录日志的实体类字段中进行注解标注。并标明对应的字段名

二、代码

1.使用LoggerUtils工具类生成日志

  public JsonResult savePrice(Price price)
    {if (price.getId() != null)
        {
            String message = LoggerUtils.buildUpdateLog(priceMapper
                    .getPriceById(price.getId()), price, Price.class);
            priceMapper.updatePrice(price);
            portalLog.log(price.getId(), message);
        }
        else
        {
            priceMapper.createPrice(price);
            portalLog.log(price.getId(), LoggerUtils.buildInsertLog(price, Price.class));
        }
        return JsonResult.success("操作成功");
    }

 

2.在Price实体类中加入注解【@LogProperty("办公区域")】,并标识名字【办公区域】

@Data
public class Price implements Serializable
{
    /**
     * 主键
     */
    private Integer id;

    /**
     * 办公区域
     */
    @LogProperty("办公区域")
    private Integer officeAreaId;
}

 

3.日志工具类【LoggerUtils】

BusinessException 可以调整成别的抛出异常,或者可以直接抛出去到业务层处理
import org.apache.commons.lang3.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 记录操作日志信息
 *
 * @author 王子威 2024年6月1日
 * @version V100R001
 * @since V100R001C001
 */
public class LoggerUtils
{
    /**
     * 新增
     */
    private final static String CREATE = "新增:";

    /**
     * 修改
     */
    private final static String UPDATE = "修改:";

    /**
     * 生成 对象更新 的变更日志信息
     *
     * @param dataToUpdate 待更新到数据库对象
     * @param dataDb       数据对象
     * @param targetClazz  目标类
     * @return 日志信息
     */
    public static String buildUpdateLog(Object dataToUpdate, Object dataDb, Class targetClazz)
    {
        if (null == dataToUpdate || null == dataDb || null == targetClazz)
        {
            return "";
        }
        StringBuilder logMsg = new StringBuilder();
        String details = StringUtils.EMPTY;

        try
        {
            // 要记录日志的 字段集合
            List<Field> targetList = new ArrayList<Field>();
            Field[] declaredFields = targetClazz.getDeclaredFields();
            for (Field field : declaredFields)
            {
                // 获取注解
                Annotation logPropertyAnnotation = field.getAnnotation(LogProperty.class);
                if (logPropertyAnnotation != null)
                {
                    // 放开权限
                    field.setAccessible(true);
                    // 获取注解标注的字段
                    targetList.add(field);
                }
            }
            if (targetList.size() <= 0)
            {
                // 没有要记录日志的字段
                return "";
            }
            // 目标对象日志拼接
            logMsg.append(UPDATE);
            for (Field field : targetList)
            {
                // 待更新 字段值
                Object valueToUpdate = field.get(dataToUpdate);
                // 数据库字段值
                Object valueFromDB = field.get(dataDb);
                LogProperty logProperty = field.getAnnotation(LogProperty.class);
                // 字段描述信息
                String fieldMsg = logProperty.value();
                if (null != valueToUpdate && !valueToUpdate.equals(valueFromDB))
                {
                    details = "【" + fieldMsg + "】由【" + valueFromDB + "】更改为【" + valueToUpdate + "】 ";
                    logMsg.append(details);
                }
            }
        }
        catch (IllegalAccessException e)
        {
            throw new BusinessException("日志生成异常", e);
        }
        return logMsg.toString();
    }

    /**
     * 生成 对象新增日志信息
     *
     * @param dataToInsert 待更新到数据库对象
     * @param targetClazz  目标类
     * @return 日志信息
     */
    public static String buildInsertLog(Object dataToInsert, Class targetClazz)
    {
        if (null == dataToInsert || null == targetClazz)
        {
            return "";
        }
        StringBuilder logMsg = new StringBuilder();
        String details = StringUtils.EMPTY;
        try
        {
            // 要记录日志的 字段集合
            List<Field> targetList = new ArrayList<Field>();
            Field[] declaredFields = targetClazz.getDeclaredFields();
            for (Field field : declaredFields)
            {
                Annotation logPropertyAnnotation = field.getAnnotation(LogProperty.class);
                if (logPropertyAnnotation != null)
                {
                    // 放开权限
                    field.setAccessible(true);
                    targetList.add(field);
                }
            }
            if (targetList.size() <= 0)
            {
                // 没有要记录日志的字段
                return "";
            }
            // 目标对象日志拼接
            logMsg.append(CREATE);
            for (Field field : targetList)
            {
                // 待更新 字段值
                Object valueToUpdate = field.get(dataToInsert);
                LogProperty logProperty = field.getAnnotation(LogProperty.class);
                // 字段描述信息
                String fieldMsg = logProperty.value();
                if (!Objects.isNull(valueToUpdate))
                {
                    details = "【" + fieldMsg + "-" + valueToUpdate + "】";
                }
                else
                {
                    details = "【" + fieldMsg + "-" + "null】";
                }
                logMsg.append(details);
            }
        }
        catch (IllegalAccessException e)
        {
            throw new BusinessException("日志生成异常", e);
        }
        return logMsg.toString();
    }
}

 

4.日志记录注解类【LogProperty】

import java.lang.annotation.*;

/**
 * 日志记录注解
 *
 * @author 王子威  2024年6月1日
 * @version V100R001
 * @since V100R001C001
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface LogProperty
{
    /**
     * 字段描述
     *
     * @return
     */
    String value() default "";
}

三、结果

修改操作

 添加操作

 

posted @ 2024-06-05 14:32  骚哥  阅读(3)  评论(0编辑  收藏  举报