修改@OperationLogDetail注解
This commit is contained in:
@ -174,6 +174,13 @@
|
||||
<version>1.2.60</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- mongodb-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -19,10 +19,6 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface OperationLogDetail {
|
||||
|
||||
/**
|
||||
* 方法描述,可使用占位符获取参数:{{tel}}
|
||||
*/
|
||||
String detail() default "";
|
||||
|
||||
/**
|
||||
* 方法执行模块名称
|
||||
@ -30,12 +26,17 @@ public @interface OperationLogDetail {
|
||||
String operationModel() default "";
|
||||
|
||||
/**
|
||||
* 日志等级:自己定义
|
||||
* 方法描述,可使用占位符获取参数:{{tel}}
|
||||
*/
|
||||
int level() default 0;
|
||||
String detail() default "";
|
||||
|
||||
/**
|
||||
* 操作类型(enum):主要是select,insert,update,delete
|
||||
*/
|
||||
OperationType operationType() default OperationType.UNKNOWN;
|
||||
|
||||
/**
|
||||
* 日志等级:自己定义
|
||||
*/
|
||||
int level() default 2;
|
||||
}
|
||||
|
@ -1,9 +1,8 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.aop;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser;
|
||||
import com.chinaunicom.mall.ebtp.common.base.service.IBaseCacheUserService;
|
||||
import com.chinaunicom.mall.ebtp.common.log.OperationLogDetail;
|
||||
import com.chinaunicom.mall.ebtp.common.log.entity.OperationLog;
|
||||
import com.chinaunicom.mall.ebtp.common.log.service.OperationLogService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -11,13 +10,11 @@ import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -34,9 +31,9 @@ import java.util.UUID;
|
||||
@Component
|
||||
public class LogAspect {
|
||||
|
||||
@Autowired
|
||||
private OperationLogService operationLogService;
|
||||
|
||||
@Resource
|
||||
private IBaseCacheUserService cacheUserService;
|
||||
|
||||
@Pointcut("@annotation(com.chinaunicom.mall.ebtp.common.log.OperationLogDetail)")
|
||||
public void operationLog() {
|
||||
@ -48,6 +45,7 @@ public class LogAspect {
|
||||
@Around("operationLog()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object res = null;
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
try {
|
||||
res = joinPoint.proceed();
|
||||
@ -76,21 +74,12 @@ public class LogAspect {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
OperationLog operationLog = new OperationLog();
|
||||
|
||||
BaseCacheUser user = cacheUserService.getCacheUser();
|
||||
|
||||
try {
|
||||
operationLog.setRunTime(time);
|
||||
operationLog.setReturnValue(mapper.writeValueAsString(res));
|
||||
operationLog.setId(UUID.randomUUID().toString());
|
||||
operationLog.setArgs(mapper.writeValueAsString(joinPoint.getArgs()));
|
||||
operationLog.setCreateTime(new Date());
|
||||
operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName());
|
||||
|
||||
if (null != user) {
|
||||
operationLog.setUserId(user.getUserId().toString());
|
||||
operationLog.setUserName(user.getFullName());
|
||||
}
|
||||
|
||||
OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
|
||||
if (annotation != null) {
|
||||
operationLog.setLevel(annotation.level());
|
||||
@ -98,6 +87,7 @@ public class LogAspect {
|
||||
operationLog.setOperationType(annotation.operationType().getValue());
|
||||
operationLog.setOperationModel(annotation.operationModel());
|
||||
}
|
||||
operationLogService.addOperationLog(operationLog);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("日志转换json错误:" + signature.getDeclaringTypeName() + "." + signature.getName());
|
||||
}
|
||||
@ -136,7 +126,7 @@ public class LogAspect {
|
||||
|
||||
@Before("operationLog()")
|
||||
public void doBeforeAdvice(JoinPoint joinPoint) {
|
||||
log.info("进入方法前执行.....");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,6 +134,7 @@ public class LogAspect {
|
||||
*/
|
||||
@AfterThrowing("operationLog()")
|
||||
public void throwss(JoinPoint jp) {
|
||||
log.info("方法异常时执行.....");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.dao;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.common.log.entity.OperationLog;
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @author f
|
||||
*/
|
||||
public interface OperationLogDao {
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param operationLog
|
||||
*/
|
||||
void save(OperationLog operationLog);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.dao.impl;
|
||||
|
||||
import com.chinaunicom.mall.ebtp.common.log.dao.OperationLogDao;
|
||||
import com.chinaunicom.mall.ebtp.common.log.entity.OperationLog;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @author f
|
||||
*/
|
||||
@Repository
|
||||
public class OperationLogDaoImpl implements OperationLogDao {
|
||||
|
||||
@Resource
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param operationLog
|
||||
*/
|
||||
@Override
|
||||
public void save(OperationLog operationLog) {
|
||||
mongoTemplate.save(operationLog);
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -13,24 +14,36 @@ import java.util.Date;
|
||||
* @author daixc
|
||||
* @date 2020-09-07
|
||||
*/
|
||||
@Document(collection = "ebtp_log")
|
||||
@Data
|
||||
public class OperationLog implements Serializable {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 链路跟踪标识
|
||||
*/
|
||||
private String transactionId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
private String createTime;
|
||||
|
||||
/**
|
||||
* 日志等级
|
||||
*/
|
||||
private Integer level;
|
||||
|
||||
/**
|
||||
* 服务名
|
||||
*/
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 被操作的模块
|
||||
*/
|
||||
|
@ -0,0 +1,59 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.service;
|
||||
|
||||
|
||||
import com.chinaunicom.mall.ebtp.common.log.entity.OperationLog;
|
||||
import com.chinaunicom.mall.ebtp.common.log.enums.OperationType;
|
||||
|
||||
/**
|
||||
* 日志持久化
|
||||
*
|
||||
* @author f
|
||||
*/
|
||||
public interface OperationLogService {
|
||||
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param operationLog
|
||||
*/
|
||||
void addOperationLog(OperationLog operationLog);
|
||||
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationModel
|
||||
* @param describe
|
||||
* @param operationType
|
||||
*/
|
||||
void addOperationLog(String args, String returnValue, String operationModel, String describe, OperationType operationType);
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationModel
|
||||
* @param operationType
|
||||
*/
|
||||
void addOperationLog(String args, String returnValue, String operationModel, OperationType operationType);
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationType
|
||||
*/
|
||||
void addOperationLog(String args, String returnValue, OperationType operationType);
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
*/
|
||||
void addOperationLog(String args, String returnValue);
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
package com.chinaunicom.mall.ebtp.common.log.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser;
|
||||
import com.chinaunicom.mall.ebtp.common.base.service.IBaseCacheUserService;
|
||||
import com.chinaunicom.mall.ebtp.common.log.dao.OperationLogDao;
|
||||
import com.chinaunicom.mall.ebtp.common.log.entity.OperationLog;
|
||||
import com.chinaunicom.mall.ebtp.common.log.enums.OperationType;
|
||||
import com.chinaunicom.mall.ebtp.common.log.service.OperationLogService;
|
||||
import com.chinaunicom.mall.ebtp.common.util.PropertyUtils;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* 日志持久化
|
||||
*
|
||||
* @author f
|
||||
*/
|
||||
@Service
|
||||
public class OperationLogServiceImpl implements OperationLogService {
|
||||
private static final String TRANSACTION_ID = "PtxId";
|
||||
private static final String P_SPAN_ID = "PspanId";
|
||||
|
||||
|
||||
@Autowired
|
||||
private IBaseCacheUserService cacheUserService;
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
private String serviceName;
|
||||
|
||||
@Autowired
|
||||
private OperationLogDao dao;
|
||||
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void addOperationLog(OperationLog operationLog) {
|
||||
if (operationLog.getMethod() == null) {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (int i = 1; i < stackTrace.length; i++) {
|
||||
StackTraceElement element = stackTrace[i];
|
||||
if (!element.getClassName().equals(OperationLogServiceImpl.class.getName())) {
|
||||
operationLog.setMethod(element.getClassName() + "." + element.getMethodName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (operationLog.getMethod() == null) {
|
||||
StackTraceElement element = Thread.currentThread().getStackTrace()[1];
|
||||
operationLog.setMethod(element.getClassName() + "." + element.getMethodName());
|
||||
}
|
||||
|
||||
|
||||
operationLog.setId(PropertyUtils.getSnowflakeId());
|
||||
operationLog.setTransactionId(MDC.get(TRANSACTION_ID));
|
||||
operationLog.setServiceName(serviceName);
|
||||
operationLog.setCreateTime(DateUtil.now());
|
||||
dao.save(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationModel
|
||||
* @param describe
|
||||
* @param operationType
|
||||
*/
|
||||
@Override
|
||||
public void addOperationLog(String args, String returnValue, String operationModel, String describe, OperationType operationType) {
|
||||
|
||||
OperationLog operationLog = new OperationLog();
|
||||
|
||||
operationLog.setArgs(args);
|
||||
operationLog.setReturnValue(returnValue);
|
||||
|
||||
BaseCacheUser user = cacheUserService.getCacheUser();
|
||||
|
||||
if (null != user) {
|
||||
operationLog.setUserId(user.getUserId());
|
||||
operationLog.setUserName(user.getFullName());
|
||||
}
|
||||
|
||||
operationLog.setLevel(3);
|
||||
operationLog.setOperationModel(operationModel);
|
||||
operationLog.setDescribe(describe);
|
||||
operationLog.setOperationType(operationType.getValue());
|
||||
|
||||
this.addOperationLog(operationLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationModel
|
||||
* @param operationType
|
||||
*/
|
||||
@Override
|
||||
public void addOperationLog(String args, String returnValue, String operationModel, OperationType operationType) {
|
||||
addOperationLog(args, returnValue, operationModel, "", operationType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
* @param operationType
|
||||
*/
|
||||
@Override
|
||||
public void addOperationLog(String args, String returnValue, OperationType operationType) {
|
||||
addOperationLog(args, returnValue, "", operationType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param args
|
||||
* @param returnValue
|
||||
*/
|
||||
@Override
|
||||
public void addOperationLog(String args, String returnValue) {
|
||||
addOperationLog(args, returnValue, OperationType.UNKNOWN);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -107,4 +107,18 @@ public class JsonUtils {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将可变参数转化为数组类型的字符串<VO>
|
||||
* <p>Title: jsonToList</p>
|
||||
* <p>Description: </p>
|
||||
*
|
||||
* @param args 参数列表
|
||||
* @return
|
||||
*/
|
||||
public static String argsToArray(Object... args) {
|
||||
return objectToJson(args);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user