diff --git a/uboot-common/pom.xml b/uboot-common/pom.xml index 0d5169b..c3e4011 100644 --- a/uboot-common/pom.xml +++ b/uboot-common/pom.xml @@ -174,6 +174,13 @@ 1.2.60 + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/OperationLogDetail.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/OperationLogDetail.java index 19e91ac..a575b3d 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/OperationLogDetail.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/OperationLogDetail.java @@ -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; } diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/aop/LogAspect.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/aop/LogAspect.java index 35ddff0..ba74ed9 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/aop/LogAspect.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/aop/LogAspect.java @@ -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; /** *

@@ -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("方法异常时执行....."); + } + } diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/OperationLogDao.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/OperationLogDao.java new file mode 100644 index 0000000..07313e2 --- /dev/null +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/OperationLogDao.java @@ -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); +} diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/impl/OperationLogDaoImpl.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/impl/OperationLogDaoImpl.java new file mode 100644 index 0000000..288dd6f --- /dev/null +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/dao/impl/OperationLogDaoImpl.java @@ -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); + } +} diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/entity/OperationLog.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/entity/OperationLog.java index 22a8038..a352dff 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/entity/OperationLog.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/entity/OperationLog.java @@ -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; /** *

@@ -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; + /** * 被操作的模块 */ diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/OperationLogService.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/OperationLogService.java new file mode 100644 index 0000000..fab03b9 --- /dev/null +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/OperationLogService.java @@ -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); +} diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/impl/OperationLogServiceImpl.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/impl/OperationLogServiceImpl.java new file mode 100644 index 0000000..29ec82a --- /dev/null +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/log/service/impl/OperationLogServiceImpl.java @@ -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); + } + + +} diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/util/JsonUtils.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/util/JsonUtils.java index 3a2d83c..74f35da 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/util/JsonUtils.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/util/JsonUtils.java @@ -107,4 +107,18 @@ public class JsonUtils { } + /** + * 将可变参数转化为数组类型的字符串 + *

Title: jsonToList

+ *

Description:

+ * + * @param args 参数列表 + * @return + */ + public static String argsToArray(Object... args) { + return objectToJson(args); + + } + + }