mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 18:46:36 +08:00
commit:同步2.0版本(添加指定审批人功能)
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.orange.demo.common.log.annotation;
|
||||
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志记录注解。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface OperationLog {
|
||||
|
||||
/**
|
||||
* 描述。
|
||||
*/
|
||||
String description() default "";
|
||||
|
||||
/**
|
||||
* 操作类型。
|
||||
*/
|
||||
int type() default SysOperationLogType.OTHER;
|
||||
|
||||
/**
|
||||
* 是否保存应答结果。
|
||||
* 对于类似导出和文件下载之类的接口,该参与应该设置为false。
|
||||
*/
|
||||
boolean saveResponse() default true;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package com.orange.demo.common.log.aop;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.core.object.ResponseResult;
|
||||
import com.orange.demo.common.core.object.TokenData;
|
||||
import com.orange.demo.common.core.util.ContextUtil;
|
||||
import com.orange.demo.common.core.util.IpUtil;
|
||||
import com.orange.demo.common.core.util.MyCommonUtil;
|
||||
import com.orange.demo.common.log.annotation.OperationLog;
|
||||
import com.orange.demo.common.log.config.OperationLogProperties;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import com.orange.demo.common.log.model.constant.SysOperationLogType;
|
||||
import com.orange.demo.common.sequence.wrapper.IdGeneratorWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 操作日志记录处理AOP对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@Slf4j
|
||||
public class OperationLogAspect {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String serviceName;
|
||||
@Autowired
|
||||
private KafkaTemplate<String, Object> kafkaTemplate;
|
||||
@Autowired
|
||||
private OperationLogProperties properties;
|
||||
@Autowired
|
||||
private IdGeneratorWrapper idGenerator;
|
||||
|
||||
/**
|
||||
* 错误信息、请求参数和应答结果字符串的最大长度。
|
||||
*/
|
||||
private final static int MAX_LENGTH = 2000;
|
||||
|
||||
/**
|
||||
* 所有controller方法。
|
||||
*/
|
||||
@Pointcut("execution(public * com.orange.demo..controller..*(..))")
|
||||
public void operationLogPointCut() {
|
||||
// 空注释,避免sonar警告
|
||||
}
|
||||
|
||||
@Around("operationLogPointCut()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
// 计时。
|
||||
long start = System.currentTimeMillis();
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
HttpServletResponse response = ContextUtil.getHttpResponse();
|
||||
String traceId = this.getTraceId(request);
|
||||
request.setAttribute(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
// 将流水号通过应答头返回给前端,便于问题精确定位。
|
||||
response.setHeader(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
MDC.put(ApplicationConstant.HTTP_HEADER_TRACE_ID, traceId);
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
// 为log4j2日志设定变量,使日志可以输出更多有价值的信息。
|
||||
if (tokenData != null) {
|
||||
MDC.put("sessionId", tokenData.getSessionId());
|
||||
MDC.put("userId", tokenData.getUserId().toString());
|
||||
}
|
||||
String[] parameterNames = this.getParameterNames(joinPoint);
|
||||
Object[] args = joinPoint.getArgs();
|
||||
JSONObject jsonArgs = new JSONObject();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Object arg = args[i];
|
||||
if (this.isNormalArgs(arg)) {
|
||||
String parameterName = parameterNames[i];
|
||||
jsonArgs.put(parameterName, arg);
|
||||
}
|
||||
}
|
||||
String params = jsonArgs.toJSONString();
|
||||
SysOperationLog operationLog = null;
|
||||
OperationLog operationLogAnnotation = null;
|
||||
boolean saveOperationLog = properties.isEnabled();
|
||||
if (saveOperationLog) {
|
||||
operationLogAnnotation = getOperationLogAnnotation(joinPoint);
|
||||
saveOperationLog = (operationLogAnnotation != null);
|
||||
}
|
||||
if (saveOperationLog) {
|
||||
operationLog = this.buildSysOperationLog(operationLogAnnotation, joinPoint, params, traceId, tokenData);
|
||||
}
|
||||
Object result;
|
||||
log.info("开始请求,url={}, reqData={}", request.getRequestURI(), params);
|
||||
try {
|
||||
// 调用原来的方法
|
||||
result = joinPoint.proceed();
|
||||
String respData = result == null ? "null" : JSON.toJSONString(result);
|
||||
Long elapse = System.currentTimeMillis() - start;
|
||||
if (saveOperationLog) {
|
||||
this.operationLogPostProcess(operationLogAnnotation, respData, operationLog, result);
|
||||
}
|
||||
log.info("请求完成, url={},elapse={}ms, respData={}", request.getRequestURI(), elapse, respData);
|
||||
} catch (Exception e) {
|
||||
if (saveOperationLog) {
|
||||
operationLog.setSuccess(false);
|
||||
operationLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, MAX_LENGTH));
|
||||
}
|
||||
log.error("请求报错,url={}, reqData={}, error={}", request.getRequestURI(), params, e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
if (saveOperationLog) {
|
||||
operationLog.setElapse(System.currentTimeMillis() - start);
|
||||
kafkaTemplate.send(properties.getKafkaTopic(), JSON.toJSONString(operationLog));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private SysOperationLog buildSysOperationLog(
|
||||
OperationLog operationLogAnnotation,
|
||||
ProceedingJoinPoint joinPoint,
|
||||
String params,
|
||||
String traceId,
|
||||
TokenData tokenData) {
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
SysOperationLog operationLog = new SysOperationLog();
|
||||
operationLog.setLogId(idGenerator.nextLongId());
|
||||
operationLog.setTraceId(traceId);
|
||||
operationLog.setDescription(operationLogAnnotation.description());
|
||||
operationLog.setOperationType(operationLogAnnotation.type());
|
||||
operationLog.setServiceName(this.serviceName);
|
||||
operationLog.setApiClass(joinPoint.getTarget().getClass().getName());
|
||||
operationLog.setApiMethod(operationLog.getApiClass() + "." + joinPoint.getSignature().getName());
|
||||
operationLog.setRequestMethod(request.getMethod());
|
||||
operationLog.setRequestUrl(request.getRequestURI());
|
||||
if (tokenData != null) {
|
||||
operationLog.setRequestIp(tokenData.getLoginIp());
|
||||
} else {
|
||||
operationLog.setRequestIp(IpUtil.getRemoteIpAddress(request));
|
||||
}
|
||||
operationLog.setOperationTime(new Date());
|
||||
if (params != null) {
|
||||
if (params.length() <= MAX_LENGTH) {
|
||||
operationLog.setRequestArguments(params);
|
||||
} else {
|
||||
operationLog.setRequestArguments(StringUtils.substring(params, 0, MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
if (tokenData != null) {
|
||||
// 对于非多租户系统,该值为空可以忽略。
|
||||
operationLog.setTenantId(tokenData.getTenantId());
|
||||
operationLog.setSessionId(tokenData.getSessionId());
|
||||
operationLog.setOperatorId(tokenData.getUserId());
|
||||
operationLog.setOperatorName(tokenData.getLoginName());
|
||||
}
|
||||
return operationLog;
|
||||
}
|
||||
|
||||
private void operationLogPostProcess(
|
||||
OperationLog operationLogAnnotation, String respData, SysOperationLog operationLog, Object result) {
|
||||
if (operationLogAnnotation.saveResponse()) {
|
||||
if (respData.length() <= MAX_LENGTH) {
|
||||
operationLog.setResponseResult(respData);
|
||||
} else {
|
||||
operationLog.setResponseResult(StringUtils.substring(respData, 0, MAX_LENGTH));
|
||||
}
|
||||
}
|
||||
// 处理大部分返回ResponseResult的接口。
|
||||
if (!(result instanceof ResponseResult)) {
|
||||
if (ContextUtil.hasRequestContext()) {
|
||||
operationLog.setSuccess(ContextUtil.getHttpResponse().getStatus() == HttpServletResponse.SC_OK);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ResponseResult<?> responseResult = (ResponseResult<?>) result;
|
||||
operationLog.setSuccess(responseResult.isSuccess());
|
||||
if (!responseResult.isSuccess()) {
|
||||
operationLog.setErrorMsg(responseResult.getErrorMessage());
|
||||
}
|
||||
if (operationLog.getOperationType().equals(SysOperationLogType.LOGIN)) {
|
||||
// 对于登录操作,由于在调用登录方法之前,没有可用的TokenData。
|
||||
// 因此如果登录成功,可再次通过TokenData.takeFromRequest()获取TokenData。
|
||||
if (operationLog.getSuccess()) {
|
||||
// 这里为了保证LoginController.doLogin方法,一定将TokenData存入Request.Attribute之中,
|
||||
// 我们将不做空值判断,一旦出错,开发者可在调试时立刻发现异常,并根据这里的注释进行修复。
|
||||
TokenData tokenData = TokenData.takeFromRequest();
|
||||
// 对于非多租户系统,为了保证代码一致性,仍可保留对tenantId的赋值代码。
|
||||
operationLog.setTenantId(tokenData.getTenantId());
|
||||
operationLog.setSessionId(tokenData.getSessionId());
|
||||
operationLog.setOperatorId(tokenData.getUserId());
|
||||
operationLog.setOperatorName(tokenData.getLoginName());
|
||||
} else {
|
||||
HttpServletRequest request = ContextUtil.getHttpRequest();
|
||||
// 登录操作需要特殊处理,无论是登录成功还是失败,都要记录operator_name字段。
|
||||
operationLog.setOperatorName(request.getParameter("loginName"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String[] getParameterNames(ProceedingJoinPoint joinPoint) {
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
return methodSignature.getParameterNames();
|
||||
}
|
||||
|
||||
private OperationLog getOperationLogAnnotation(JoinPoint joinPoint) throws Exception {
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
Method method = methodSignature.getMethod();
|
||||
return method.getAnnotation(OperationLog.class);
|
||||
}
|
||||
|
||||
private String getTraceId(HttpServletRequest request) {
|
||||
// 获取请求流水号。
|
||||
// 对于微服务系统,为了保证traceId在全调用链的唯一性,因此在网关的过滤器中创建了该值。
|
||||
String traceId = request.getHeader(ApplicationConstant.HTTP_HEADER_TRACE_ID);
|
||||
if (StringUtils.isBlank(traceId)) {
|
||||
traceId = MyCommonUtil.generateUuid();
|
||||
}
|
||||
return traceId;
|
||||
}
|
||||
|
||||
private boolean isNormalArgs(Object o) {
|
||||
if (o instanceof List) {
|
||||
List<?> list = (List<?>) o;
|
||||
if (CollUtil.isNotEmpty(list)) {
|
||||
return !(list.get(0) instanceof MultipartFile);
|
||||
}
|
||||
}
|
||||
return !(o instanceof HttpServletRequest)
|
||||
&& !(o instanceof HttpServletResponse)
|
||||
&& !(o instanceof MultipartFile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.orange.demo.common.log.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-log模块的自动配置引导类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@EnableConfigurationProperties({OperationLogProperties.class})
|
||||
public class CommonLogAutoConfig {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.orange.demo.common.log.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 操作日志的配置类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "common-log.operation-log")
|
||||
public class OperationLogProperties {
|
||||
|
||||
/**
|
||||
* 是否采集操作日志。
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
/**
|
||||
* kafka topic
|
||||
*/
|
||||
private String kafkaTopic = "SysOperationLog";
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.orange.demo.common.log.dao;
|
||||
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统操作日志对应的数据访问对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysOperationLogMapper extends BaseDaoMapper<SysOperationLog> {
|
||||
|
||||
/**
|
||||
* 批量插入。
|
||||
*
|
||||
* @param operationLogList 操作日志列表。
|
||||
*/
|
||||
void insertList(List<SysOperationLog> operationLogList);
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param sysOperationLogFilter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
List<SysOperationLog> getSysOperationLogList(
|
||||
@Param("sysOperationLogFilter") SysOperationLog sysOperationLogFilter,
|
||||
@Param("orderBy") String orderBy);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?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.orange.demo.common.log.dao.SysOperationLogMapper">
|
||||
<resultMap id="BaseResultMap" type="com.orange.demo.common.log.model.SysOperationLog">
|
||||
<id column="log_id" jdbcType="BIGINT" property="logId"/>
|
||||
<result column="description" jdbcType="VARCHAR" property="description"/>
|
||||
<result column="operation_type" jdbcType="INTEGER" property="operationType"/>
|
||||
<result column="service_name" jdbcType="VARCHAR" property="serviceName"/>
|
||||
<result column="api_class" jdbcType="VARCHAR" property="apiClass"/>
|
||||
<result column="api_method" jdbcType="VARCHAR" property="apiMethod"/>
|
||||
<result column="session_id" jdbcType="VARCHAR" property="sessionId"/>
|
||||
<result column="trace_id" jdbcType="VARCHAR" property="traceId"/>
|
||||
<result column="elapse" jdbcType="BIGINT" property="elapse"/>
|
||||
<result column="request_method" jdbcType="VARCHAR" property="requestMethod"/>
|
||||
<result column="request_url" jdbcType="VARCHAR" property="requestUrl"/>
|
||||
<result column="request_arguments" jdbcType="VARCHAR" property="requestArguments"/>
|
||||
<result column="response_result" jdbcType="VARCHAR" property="responseResult"/>
|
||||
<result column="request_ip" jdbcType="VARCHAR" property="requestIp"/>
|
||||
<result column="success" jdbcType="BIT" property="success"/>
|
||||
<result column="error_msg" jdbcType="VARCHAR" property="errorMsg"/>
|
||||
<result column="tenant_id" jdbcType="BIGINT" property="tenantId"/>
|
||||
<result column="operator_id" jdbcType="BIGINT" property="operatorId"/>
|
||||
<result column="operator_name" jdbcType="VARCHAR" property="operatorName"/>
|
||||
<result column="operation_time" jdbcType="TIMESTAMP" property="operationTime"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 这里仅包含调用接口输入的主表过滤条件 -->
|
||||
<sql id="filterRef">
|
||||
<if test="sysOperationLogFilter != null">
|
||||
<if test="sysOperationLogFilter.operationType != null">
|
||||
AND zz_sys_operation_log.operation_type = #{sysOperationLogFilter.operationType}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.requestUrl != null and sysOperationLogFilter.requestUrl != ''">
|
||||
<bind name = "safeRequestUrl" value = "'%' + sysOperationLogFilter.requestUrl + '%'" />
|
||||
AND zz_sys_operation_log.request_url LIKE #{safeRequestUrl}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.traceId != null and sysOperationLogFilter.traceId != ''">
|
||||
AND zz_sys_operation_log.trace_id = #{sysOperationLogFilter.traceId}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.success != null">
|
||||
AND zz_sys_operation_log.success = #{sysOperationLogFilter.success}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operatorName != null and sysOperationLogFilter.operatorName != ''">
|
||||
<bind name = "safeOperatorName" value = "'%' + sysOperationLogFilter.operatorName + '%'" />
|
||||
AND zz_sys_operation_log.operator_name LIKE #{safeOperatorName}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.elapseMin != null and sysOperationLogFilter.elapseMin != ''">
|
||||
AND zz_sys_operation_log.elapse >= #{sysOperationLogFilter.elapseMin}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.elapseMax != null and sysOperationLogFilter.elapseMax != ''">
|
||||
AND zz_sys_operation_log.elapse <= #{sysOperationLogFilter.elapseMax}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operationTimeStart != null and sysOperationLogFilter.operationTimeStart != ''">
|
||||
AND zz_sys_operation_log.operation_time >= #{sysOperationLogFilter.operationTimeStart}
|
||||
</if>
|
||||
<if test="sysOperationLogFilter.operationTimeEnd != null and sysOperationLogFilter.operationTimeEnd != ''">
|
||||
AND zz_sys_operation_log.operation_time <= #{sysOperationLogFilter.operationTimeEnd}
|
||||
</if>
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<insert id="insertList">
|
||||
INSERT INTO zz_sys_operation_log VALUES
|
||||
<foreach collection="list" index="index" item="item" separator=",">
|
||||
(
|
||||
#{item.logId},
|
||||
#{item.description},
|
||||
#{item.operationType},
|
||||
#{item.serviceName},
|
||||
#{item.apiClass},
|
||||
#{item.apiMethod},
|
||||
#{item.sessionId},
|
||||
#{item.traceId},
|
||||
#{item.elapse},
|
||||
#{item.requestMethod},
|
||||
#{item.requestUrl},
|
||||
#{item.requestArguments},
|
||||
#{item.responseResult},
|
||||
#{item.requestIp},
|
||||
#{item.success},
|
||||
#{item.errorMsg},
|
||||
#{item.tenantId},
|
||||
#{item.operatorId},
|
||||
#{item.operatorName},
|
||||
#{item.operationTime}
|
||||
)
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
<select id="getSysOperationLogList" resultMap="BaseResultMap" parameterType="com.orange.demo.common.log.model.SysOperationLog">
|
||||
SELECT * FROM zz_sys_operation_log
|
||||
<where>
|
||||
<include refid="filterRef"/>
|
||||
</where>
|
||||
<if test="orderBy != null and orderBy != ''">
|
||||
ORDER BY ${orderBy}
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.orange.demo.common.log.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.orange.demo.common.core.annotation.TenantFilterColumn;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 操作日志记录表
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@TableName("zz_sys_operation_log")
|
||||
public class SysOperationLog {
|
||||
|
||||
/**
|
||||
* 主键Id。
|
||||
*/
|
||||
@TableId(value = "log_id")
|
||||
private Long logId;
|
||||
|
||||
/**
|
||||
* 日志描述。
|
||||
*/
|
||||
@TableField(value = "description")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 操作类型。
|
||||
* 常量值定义可参考SysOperationLogType对象。
|
||||
*/
|
||||
@TableField(value = "operation_type")
|
||||
private Integer operationType;
|
||||
|
||||
/**
|
||||
* 接口所在服务名称。
|
||||
* 通常为spring.application.name配置项的值。
|
||||
*/
|
||||
@TableField(value = "service_name")
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 调用的controller全类名。
|
||||
* 之所以为独立字段,是为了便于查询和统计接口的调用频度。
|
||||
*/
|
||||
@TableField(value = "api_class")
|
||||
private String apiClass;
|
||||
|
||||
/**
|
||||
* 调用的controller中的方法。
|
||||
* 格式为:接口类名 + "." + 方法名。
|
||||
*/
|
||||
@TableField(value = "api_method")
|
||||
private String apiMethod;
|
||||
|
||||
/**
|
||||
* 用户会话sessionId。
|
||||
* 主要是为了便于统计,以及跟踪查询定位问题。
|
||||
*/
|
||||
@TableField(value = "session_id")
|
||||
private String sessionId;
|
||||
|
||||
/**
|
||||
* 每次请求的Id。
|
||||
* 对于微服务之间的调用,在同一个请求的调用链中,该值是相同的。
|
||||
*/
|
||||
@TableField(value = "trace_id")
|
||||
private String traceId;
|
||||
|
||||
/**
|
||||
* 调用时长。
|
||||
*/
|
||||
@TableField(value = "elapse")
|
||||
private Long elapse;
|
||||
|
||||
/**
|
||||
* HTTP 请求方法,如GET。
|
||||
*/
|
||||
@TableField(value = "request_method")
|
||||
private String requestMethod;
|
||||
|
||||
/**
|
||||
* HTTP 请求地址。
|
||||
*/
|
||||
@TableField(value = "request_url")
|
||||
private String requestUrl;
|
||||
|
||||
/**
|
||||
* controller接口参数。
|
||||
*/
|
||||
@TableField(value = "request_arguments")
|
||||
private String requestArguments;
|
||||
|
||||
/**
|
||||
* controller应答结果。
|
||||
*/
|
||||
@TableField(value = "response_result")
|
||||
private String responseResult;
|
||||
|
||||
/**
|
||||
* 请求IP。
|
||||
*/
|
||||
@TableField(value = "request_ip")
|
||||
private String requestIp;
|
||||
|
||||
/**
|
||||
* 应答状态。
|
||||
*/
|
||||
@TableField(value = "success")
|
||||
private Boolean success;
|
||||
|
||||
/**
|
||||
* 错误信息。
|
||||
*/
|
||||
@TableField(value = "error_msg")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 租户Id。
|
||||
* 仅用于多租户系统,是便于进行对租户的操作查询和统计分析。
|
||||
*/
|
||||
@TenantFilterColumn
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
|
||||
/**
|
||||
* 操作员Id。
|
||||
*/
|
||||
@TableField(value = "operator_id")
|
||||
private Long operatorId;
|
||||
|
||||
/**
|
||||
* 操作员名称。
|
||||
*/
|
||||
@TableField(value = "operator_name")
|
||||
private String operatorName;
|
||||
|
||||
/**
|
||||
* 操作时间。
|
||||
*/
|
||||
@TableField(value = "operation_time")
|
||||
private Date operationTime;
|
||||
|
||||
/**
|
||||
* 调用时长最小值。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private Long elapseMin;
|
||||
|
||||
/**
|
||||
* 调用时长最大值。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private Long elapseMax;
|
||||
|
||||
/**
|
||||
* 操作开始时间。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private String operationTimeStart;
|
||||
|
||||
/**
|
||||
* 操作结束时间。
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private String operationTimeEnd;
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.orange.demo.common.log.model.constant;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 操作日志类型常量字典对象。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public final class SysOperationLogType {
|
||||
|
||||
/**
|
||||
* 其他。
|
||||
*/
|
||||
public static final int OTHER = -1;
|
||||
/**
|
||||
* 登录。
|
||||
*/
|
||||
public static final int LOGIN = 0;
|
||||
/**
|
||||
* 登出。
|
||||
*/
|
||||
public static final int LOGOUT = 5;
|
||||
/**
|
||||
* 新增。
|
||||
*/
|
||||
public static final int ADD = 10;
|
||||
/**
|
||||
* 修改。
|
||||
*/
|
||||
public static final int UPDATE = 15;
|
||||
/**
|
||||
* 删除。
|
||||
*/
|
||||
public static final int DELETE = 20;
|
||||
/**
|
||||
* 新增多对多关联。
|
||||
*/
|
||||
public static final int ADD_M2M = 25;
|
||||
/**
|
||||
* 移除多对多关联。
|
||||
*/
|
||||
public static final int DELETE_M2M = 30;
|
||||
/**
|
||||
* 查询。
|
||||
*/
|
||||
public static final int LIST = 35;
|
||||
/**
|
||||
* 分组查询。
|
||||
*/
|
||||
public static final int LIST_WITH_GROUP = 40;
|
||||
/**
|
||||
* 导出。
|
||||
*/
|
||||
public static final int EXPORT = 45;
|
||||
/**
|
||||
* 上传。
|
||||
*/
|
||||
public static final int UPLOAD = 50;
|
||||
/**
|
||||
* 下载。
|
||||
*/
|
||||
public static final int DOWNLOAD = 55;
|
||||
/**
|
||||
* 重置缓存。
|
||||
*/
|
||||
public static final int RELOAD_CACHE = 60;
|
||||
/**
|
||||
* 发布。
|
||||
*/
|
||||
public static final int PUBLISH = 65;
|
||||
/**
|
||||
* 取消发布。
|
||||
*/
|
||||
public static final int UNPUBLISH = 70;
|
||||
/**
|
||||
* 暂停。
|
||||
*/
|
||||
public static final int SUSPEND = 75;
|
||||
/**
|
||||
* 恢复。
|
||||
*/
|
||||
public static final int RESUME = 80;
|
||||
/**
|
||||
* 启动流程。
|
||||
*/
|
||||
public static final int START_PROCESS = 100;
|
||||
/**
|
||||
* 停止流程。
|
||||
*/
|
||||
public static final int STOP_PROCESS = 105;
|
||||
/**
|
||||
* 删除流程。
|
||||
*/
|
||||
public static final int DELETE_PROCESS = 110;
|
||||
/**
|
||||
* 取消流程。
|
||||
*/
|
||||
public static final int CANCEL_PROCESS = 115;
|
||||
/**
|
||||
* 提交任务。
|
||||
*/
|
||||
public static final int SUBMIT_TASK = 120;
|
||||
|
||||
private static final Map<Object, String> DICT_MAP = new HashMap<>(15);
|
||||
static {
|
||||
DICT_MAP.put(OTHER, "其他");
|
||||
DICT_MAP.put(LOGIN, "登录");
|
||||
DICT_MAP.put(LOGOUT, "登出");
|
||||
DICT_MAP.put(ADD, "新增");
|
||||
DICT_MAP.put(UPDATE, "修改");
|
||||
DICT_MAP.put(DELETE, "删除");
|
||||
DICT_MAP.put(ADD_M2M, "新增多对多关联");
|
||||
DICT_MAP.put(DELETE_M2M, "移除多对多关联");
|
||||
DICT_MAP.put(LIST, "查询");
|
||||
DICT_MAP.put(LIST_WITH_GROUP, "分组查询");
|
||||
DICT_MAP.put(EXPORT, "导出");
|
||||
DICT_MAP.put(UPLOAD, "上传");
|
||||
DICT_MAP.put(DOWNLOAD, "下载");
|
||||
DICT_MAP.put(RELOAD_CACHE, "重置缓存");
|
||||
DICT_MAP.put(PUBLISH, "发布");
|
||||
DICT_MAP.put(UNPUBLISH, "取消发布");
|
||||
DICT_MAP.put(SUSPEND, "暂停");
|
||||
DICT_MAP.put(RESUME, "恢复");
|
||||
DICT_MAP.put(START_PROCESS, "启动流程");
|
||||
DICT_MAP.put(STOP_PROCESS, "停止流程");
|
||||
DICT_MAP.put(DELETE_PROCESS, "删除流程");
|
||||
DICT_MAP.put(CANCEL_PROCESS, "取消流程");
|
||||
DICT_MAP.put(SUBMIT_TASK, "提交任务");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断参数是否为当前常量字典的合法值。
|
||||
*
|
||||
* @param value 待验证的参数值。
|
||||
* @return 合法返回true,否则false。
|
||||
*/
|
||||
public static boolean isValid(Integer value) {
|
||||
return value != null && DICT_MAP.containsKey(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造函数,明确标识该常量类的作用。
|
||||
*/
|
||||
private SysOperationLogType() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.orange.demo.common.log.service;
|
||||
|
||||
import com.orange.demo.common.core.base.service.IBaseService;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志服务接口。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
public interface SysOperationLogService extends IBaseService<SysOperationLog, Long> {
|
||||
|
||||
/**
|
||||
* 异步的插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
void saveNewAsync(SysOperationLog operationLog);
|
||||
|
||||
/**
|
||||
* 插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
void saveNew(SysOperationLog operationLog);
|
||||
|
||||
/**
|
||||
* 批量插入。
|
||||
*
|
||||
* @param sysOperationLogList 操作日志列表。
|
||||
*/
|
||||
void batchSave(List<SysOperationLog> sysOperationLogList);
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param filter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
List<SysOperationLog> getSysOperationLogList(SysOperationLog filter, String orderBy);
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.orange.demo.common.log.service.impl;
|
||||
|
||||
import com.orange.demo.common.core.annotation.MyDataSource;
|
||||
import com.orange.demo.common.core.base.dao.BaseDaoMapper;
|
||||
import com.orange.demo.common.core.base.service.BaseService;
|
||||
import com.orange.demo.common.core.constant.ApplicationConstant;
|
||||
import com.orange.demo.common.log.dao.SysOperationLogMapper;
|
||||
import com.orange.demo.common.log.model.SysOperationLog;
|
||||
import com.orange.demo.common.log.service.SysOperationLogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志服务实现类。
|
||||
* 这里需要重点解释下MyDataSource注解。在单数据源服务中,由于没有DataSourceAspect的切面类,所以该注解不会
|
||||
* 有任何作用和影响。然而在多数据源情况下,由于每个服务都有自己的DataSourceType常量对象,表示不同的数据源。
|
||||
* 而common-log在公用模块中,不能去依赖业务服务,因此这里给出了一个固定值。我们在业务的DataSourceType中,也要
|
||||
* 使用该值ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE,去关联操作日志所需的数据源配置。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@MyDataSource(ApplicationConstant.OPERATION_LOG_DATASOURCE_TYPE)
|
||||
@Service
|
||||
public class SysOperationLogServiceImpl extends BaseService<SysOperationLog, Long> implements SysOperationLogService {
|
||||
|
||||
@Autowired
|
||||
private SysOperationLogMapper sysOperationLogMapper;
|
||||
|
||||
@Override
|
||||
protected BaseDaoMapper<SysOperationLog> mapper() {
|
||||
return sysOperationLogMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步插入一条新操作日志。通常用于在橙单中创建的单体工程服务。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
@Async
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveNewAsync(SysOperationLog operationLog) {
|
||||
sysOperationLogMapper.insert(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入一条新操作日志。
|
||||
*
|
||||
* @param operationLog 操作日志对象。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void saveNew(SysOperationLog operationLog) {
|
||||
sysOperationLogMapper.insert(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入。通常用于在橙单中创建的微服务工程服务。
|
||||
*
|
||||
* @param sysOperationLogList 操作日志列表。
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void batchSave(List<SysOperationLog> sysOperationLogList) {
|
||||
sysOperationLogMapper.insertList(sysOperationLogList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据过滤条件和排序规则,查询操作日志。
|
||||
*
|
||||
* @param filter 操作日志的过滤对象。
|
||||
* @param orderBy 排序规则。
|
||||
* @return 查询列表。
|
||||
*/
|
||||
@Override
|
||||
public List<SysOperationLog> getSysOperationLogList(SysOperationLog filter, String orderBy) {
|
||||
return sysOperationLogMapper.getSysOperationLogList(filter, orderBy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.orange.demo.common.log.config.CommonLogAutoConfig
|
||||
Reference in New Issue
Block a user