日志记录
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package cn.ysk.cashier.service;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class SpelUtil {
|
||||
/**
|
||||
* 用于SpEL表达式解析.
|
||||
*/
|
||||
private static final SpelExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
/**
|
||||
* 用于获取方法参数定义名字.
|
||||
*/
|
||||
private static final DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
||||
|
||||
/**
|
||||
* 解析SpEL表达式
|
||||
*
|
||||
* @param spELStr
|
||||
* @param joinPoint
|
||||
* @return
|
||||
*/
|
||||
public static String generateKeyBySpEL(String spELStr, ProceedingJoinPoint joinPoint) {
|
||||
// 通过joinPoint获取被注解方法
|
||||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = methodSignature.getMethod();
|
||||
// 使用Spring的DefaultParameterNameDiscoverer获取方法形参名数组
|
||||
String[] paramNames = nameDiscoverer.getParameterNames(method);
|
||||
// 解析过后的Spring表达式对象
|
||||
Expression expression = parser.parseExpression(spELStr);
|
||||
// Spring的表达式上下文对象
|
||||
EvaluationContext context = new StandardEvaluationContext();
|
||||
// 通过joinPoint获取被注解方法的形参
|
||||
Object[] args = joinPoint.getArgs();
|
||||
// 给上下文赋值
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
context.setVariable(paramNames[i], args[i]);
|
||||
}
|
||||
if(expression.getValue(context)==null){
|
||||
return "";
|
||||
}
|
||||
return expression.getValue(context).toString();
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import cn.ysk.cashier.domain.Log;
|
||||
import cn.ysk.cashier.domain.LogVO;
|
||||
import cn.ysk.cashier.repository.LogRepository;
|
||||
import cn.ysk.cashier.service.LogService;
|
||||
import cn.ysk.cashier.service.SpelUtil;
|
||||
import cn.ysk.cashier.service.dto.LogQueryCriteria;
|
||||
import cn.ysk.cashier.service.dto.LogQueryCriteriaExt;
|
||||
import cn.ysk.cashier.service.mapstruct.LogErrorMapper;
|
||||
@@ -80,7 +81,7 @@ public class LogServiceImpl implements LogService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log,Integer shopId) {
|
||||
public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log, Integer shopId) {
|
||||
if (log == null) {
|
||||
throw new IllegalArgumentException("Log 不能为 null!");
|
||||
}
|
||||
@@ -90,17 +91,21 @@ public class LogServiceImpl implements LogService {
|
||||
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
|
||||
|
||||
// 描述
|
||||
log.setDescription(aopLog.value());
|
||||
|
||||
String[] split = aopLog.value().split(":");
|
||||
if (split.length == 2) {
|
||||
String value = SpelUtil.generateKeyBySpEL(split[1], joinPoint);
|
||||
// 描述
|
||||
log.setDescription(split[0] + ":" + value);
|
||||
}else {
|
||||
log.setDescription(split[0]);
|
||||
}
|
||||
log.setRequestIp(ip);
|
||||
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
|
||||
log.setMethod(methodName);
|
||||
log.setUsername(username);
|
||||
log.setParams(getParameter(method, joinPoint.getArgs()));
|
||||
// 记录登录用户,隐藏密码信息
|
||||
if(signature.getName().equals("login") && StringUtils.isNotEmpty(log.getParams())){
|
||||
if (signature.getName().equals("login") && StringUtils.isNotEmpty(log.getParams())) {
|
||||
JSONObject obj = JSONUtil.parseObj(log.getParams());
|
||||
log.setUsername(obj.getStr("username", ""));
|
||||
log.setParams(JSONUtil.toJsonStr(Dict.create().set("username", log.getUsername())));
|
||||
@@ -182,7 +187,7 @@ public class LogServiceImpl implements LogService {
|
||||
public Map<String, Object> shopInfoLog(LogQueryCriteriaExt criteria, Pageable pageable) {
|
||||
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)), pageable);
|
||||
List<LogVO> logVOList = new ArrayList<>();
|
||||
for (Log log :page.getContent()) {
|
||||
for (Log log : page.getContent()) {
|
||||
LogVO logVO = new LogVO();
|
||||
logVO.setDescription(log.getDescription());
|
||||
logVO.setCreateTime(log.getCreateTime());
|
||||
@@ -190,9 +195,9 @@ public class LogServiceImpl implements LogService {
|
||||
logVO.setRequestIp(log.getRequestIp());
|
||||
logVOList.add(logVO);
|
||||
}
|
||||
Map<String,Object> map = new LinkedHashMap<>(2);
|
||||
map.put("content",logVOList);
|
||||
map.put("totalElements",page.getTotalElements());
|
||||
Map<String, Object> map = new LinkedHashMap<>(2);
|
||||
map.put("content", logVOList);
|
||||
map.put("totalElements", page.getTotalElements());
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user