日志记录v1.0
This commit is contained in:
parent
e0b8a57c0d
commit
3587eddc75
|
|
@ -1,5 +1,7 @@
|
|||
package com.engine.organization.annotation;
|
||||
|
||||
import com.engine.organization.enums.OperateTypeEnum;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
|
|
@ -15,12 +17,12 @@ import java.lang.annotation.*;
|
|||
public @interface Log {
|
||||
|
||||
/**
|
||||
* 操作事件
|
||||
* 操作类型
|
||||
*/
|
||||
String value();
|
||||
OperateTypeEnum operateType();
|
||||
|
||||
/**
|
||||
* 字段组装描述内容
|
||||
* 操作说明
|
||||
*/
|
||||
String description() default "";
|
||||
String operateDesc() default "";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,19 +27,35 @@ public class LoggerContext implements Serializable {
|
|||
//日志ID
|
||||
private long id;
|
||||
|
||||
//操作详细说明
|
||||
private String operateDesc;
|
||||
|
||||
//操作人
|
||||
private String operator;
|
||||
private long operatorId;
|
||||
|
||||
//操作人姓名
|
||||
private String operatorName;
|
||||
|
||||
// '操作时间(yyyy-MM-dd HH:mm:ss)'
|
||||
// 创建时间(yyyy-MM-dd HH:mm:ss)
|
||||
@JSONField(format="yyyy-MM-dd HH:mm:ss")
|
||||
private Date operTime;
|
||||
private Date createTime;
|
||||
|
||||
private Integer operateType;
|
||||
//操作类型增删改查等
|
||||
private String operateType;
|
||||
|
||||
//涉及的相关参数-转string存储
|
||||
private String paramsStr;
|
||||
|
||||
//客户端Ip
|
||||
private String clientIp;
|
||||
|
||||
// 请求方法名
|
||||
private String methodName;
|
||||
|
||||
//请求类名称
|
||||
private String className;
|
||||
|
||||
//是否删除
|
||||
private int deleteType;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package com.engine.organization.enums;
|
||||
|
||||
/**
|
||||
* @Author weaver_cl
|
||||
* @Description: TODO
|
||||
* @Date 2022/4/28
|
||||
* @Version V1.0
|
||||
**/
|
||||
public enum DeleteTypeEnum {
|
||||
|
||||
NOT_DELETED(0, "未删除"),
|
||||
|
||||
DELETED(1, "已删除");
|
||||
|
||||
private Integer value;
|
||||
|
||||
private String desc;
|
||||
|
||||
DeleteTypeEnum(Integer value, String desc) {
|
||||
this.value = value;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.engine.organization.mapper.SISLog;
|
||||
|
||||
import com.engine.organization.entity.LoggerContext;
|
||||
|
||||
/**
|
||||
* @Author weaver_cl
|
||||
* @Description: TODO
|
||||
* @Date 2022/4/28
|
||||
* @Version V1.0
|
||||
**/
|
||||
public interface SISLogMapper {
|
||||
|
||||
void insert(LoggerContext loggerContext);
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.engine.organization.mapper.SISLog.SISLogMapper">
|
||||
<resultMap id="BaseResultMap" type="com.engine.organization.entity.LoggerContext">
|
||||
<!--<result column="id" property="id"/>-->
|
||||
<!--<result column="operate_desc" property="operateDesc"/>-->
|
||||
<!--<result column="operator_id" property="operatorId"/>-->
|
||||
<!--<result column="operator_name" property="operatorName"/>-->
|
||||
<!--<result column="create_time" property="createTime"/>-->
|
||||
<!--<result column="operate_type" property="operateType"/>-->
|
||||
<!--<result column="params_str" property="paramsStr"/>-->
|
||||
<!--<result column="client_ip" property="clientIp"/>-->
|
||||
<!--<result column="method_name" property="methodName"/>-->
|
||||
<!--<result column="class_name" property="className"/>-->
|
||||
<!--<result column="delete_type" property="deleteType"/>-->
|
||||
</resultMap>
|
||||
|
||||
<!-- 表字段 -->
|
||||
<sql id="baseColumns">
|
||||
t.id
|
||||
, t.operate_desc
|
||||
, t.operator_id
|
||||
, t.operator_name
|
||||
, t.create_time
|
||||
, t.operate_type
|
||||
, t.params_str
|
||||
, t.client_ip
|
||||
, t.method_name
|
||||
, t.class_name
|
||||
, t.delete_type
|
||||
</sql>
|
||||
|
||||
<!-- 插入全部字段 -->
|
||||
<insert id="insert" parameterType="com.engine.organization.entity.LoggerContext"
|
||||
keyProperty="id" keyColumn="id" useGeneratedKeys="true"
|
||||
>
|
||||
INSERT INTO hr_log
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
operate_desc,
|
||||
operator_id,
|
||||
operator_name,
|
||||
create_time,
|
||||
operate_type,
|
||||
params_str,
|
||||
client_ip,
|
||||
method_name,
|
||||
class_name,
|
||||
delete_type
|
||||
</trim>
|
||||
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
|
||||
#{operateDesc},
|
||||
#{operatorId},
|
||||
#{operatorName},
|
||||
#{createTime},
|
||||
#{operateType},
|
||||
#{paramsStr},
|
||||
#{clientIp},
|
||||
#{methodName},
|
||||
#{className},
|
||||
#{deleteType}
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</mapper>
|
||||
|
|
@ -1,12 +1,18 @@
|
|||
package com.engine.organization.service.impl;
|
||||
|
||||
import com.engine.core.impl.Service;
|
||||
import com.engine.organization.annotation.Log;
|
||||
import com.engine.organization.entity.LoggerContext;
|
||||
import com.engine.organization.entity.po.Demo;
|
||||
import com.engine.organization.enums.OperateTypeEnum;
|
||||
import com.engine.organization.mapper.DemoMapper;
|
||||
import com.engine.organization.service.DemoService;
|
||||
import com.engine.organization.util.HrmI18nUtil;
|
||||
import com.engine.organization.util.LogAspect;
|
||||
import com.engine.organization.util.OrganizationAssert;
|
||||
import com.engine.organization.util.db.MapperProxyFactory;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -17,10 +23,21 @@ import java.util.List;
|
|||
**/
|
||||
public class DemoServiceImpl extends Service implements DemoService {
|
||||
|
||||
@Override
|
||||
public List<Demo> list(String name) {
|
||||
OrganizationAssert.notNull(name,"参数不能为空");
|
||||
return MapperProxyFactory.getProxy(DemoMapper.class).listAll();
|
||||
|
||||
|
||||
@Override
|
||||
@Log(operateType = OperateTypeEnum.ADD,operateDesc = "测试自定义日志")
|
||||
public List<Demo> list(String name) {
|
||||
|
||||
OrganizationAssert.notNull(name,HrmI18nUtil.getI18nLabel(34721,"参数不能为空") );
|
||||
List<Demo> demos = MapperProxyFactory.getProxy(DemoMapper.class).listAll();
|
||||
|
||||
//操作日志记录
|
||||
Method method = new Object(){}.getClass().getEnclosingMethod();
|
||||
LoggerContext loggerContext = LoggerContext.builder().operatorId(user.getUID()).operatorName(user.getLastname()).build();
|
||||
LogAspect logAspect = new LogAspect(DemoServiceImpl.class,method,loggerContext);
|
||||
logAspect.start();
|
||||
|
||||
return demos;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,40 +1,70 @@
|
|||
package com.engine.organization.util;
|
||||
|
||||
import com.engine.organization.annotation.Log;
|
||||
import com.engine.organization.entity.LoggerContext;
|
||||
import com.engine.organization.enums.DeleteTypeEnum;
|
||||
import com.engine.organization.mapper.SISLog.SISLogMapper;
|
||||
import com.engine.organization.util.db.MapperProxyFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* @Author weaver_cl
|
||||
* @Description: TODO
|
||||
* @Description: TODO 日志切面操作
|
||||
* @Date 2022/4/27
|
||||
* @Version V1.0
|
||||
**/
|
||||
public class LogAspect<T> {
|
||||
public class LogAspect<T> {
|
||||
|
||||
Class<T> clazz;
|
||||
|
||||
Method method;
|
||||
|
||||
public LogAspect(Class<T> clazz,Method method) {
|
||||
LoggerContext loggerContext;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LogAspect.class);
|
||||
|
||||
|
||||
public LogAspect(Class<T> clazz,Method method,LoggerContext loggerContext) {
|
||||
this.clazz = clazz;
|
||||
this.method = method;
|
||||
this.loggerContext = loggerContext;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
boolean f = method.isAnnotationPresent(Log.class);
|
||||
//线程池
|
||||
ExecutorService taskExecutor = Executors.newCachedThreadPool();
|
||||
taskExecutor.execute(() -> {
|
||||
boolean f = method.isAnnotationPresent(Log.class);
|
||||
if (f) {
|
||||
Log annotation = method.getAnnotation(Log.class);
|
||||
Parameter[] parameters = method.getParameters();
|
||||
String value = annotation.operateType().getValue();
|
||||
String operateDesc = annotation.operateDesc();
|
||||
loggerContext.setOperateDesc(operateDesc);
|
||||
loggerContext.setCreateTime(new Date());
|
||||
loggerContext.setOperateType(value);
|
||||
loggerContext.setParamsStr(Arrays.toString(parameters));
|
||||
loggerContext.setClientIp("127.0.0.1");
|
||||
loggerContext.setMethodName(method.getName());
|
||||
loggerContext.setClassName(clazz.getName());
|
||||
loggerContext.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue());
|
||||
MapperProxyFactory.getProxy(SISLogMapper.class).insert(loggerContext);
|
||||
}
|
||||
});
|
||||
|
||||
if (f) {
|
||||
Log annotation = method.getAnnotation(Log.class);
|
||||
String value = annotation.value();
|
||||
String description = annotation.description();
|
||||
}
|
||||
|
||||
Method[] methods = clazz.getMethods();
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
// 记录本地异常日志
|
||||
logger.error("后置通知异常:异常信息:", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue