1.0版本完成
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
package co.yixiang.service;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.service.dto.LogQueryCriteria;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
public interface LogService {
|
||||
|
||||
/**
|
||||
* queryAll
|
||||
* @param criteria
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Object queryAll(LogQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* queryAllByUser
|
||||
* @param criteria
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 新增日志
|
||||
* @param username
|
||||
* @param ip
|
||||
* @param joinPoint
|
||||
* @param log
|
||||
*/
|
||||
@Async
|
||||
void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log);
|
||||
|
||||
/**
|
||||
* 查询异常详情
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Object findByErrDetail(Long id);
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package co.yixiang.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
public class LogErrorDTO implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 操作用户
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 方法名
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String params;
|
||||
|
||||
/**
|
||||
* 请求ip
|
||||
*/
|
||||
private String requestIp;
|
||||
|
||||
private String address;
|
||||
|
||||
|
||||
/**
|
||||
* 创建日期
|
||||
*/
|
||||
private Timestamp createTime;
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package co.yixiang.service.dto;
|
||||
|
||||
import co.yixiang.annotation.Query;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 日志查询类
|
||||
* @author Zheng Jie
|
||||
* @date 2019-6-4 09:23:07
|
||||
*/
|
||||
@Data
|
||||
public class LogQueryCriteria {
|
||||
|
||||
// 多字段模糊
|
||||
@Query(blurry = "username,description,address,requestIp,method,params")
|
||||
private String blurry;
|
||||
|
||||
@Query
|
||||
private String logType;
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
package co.yixiang.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Data
|
||||
public class LogSmallDTO implements Serializable {
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 请求ip
|
||||
*/
|
||||
private String requestIp;
|
||||
|
||||
/**
|
||||
* 请求耗时
|
||||
*/
|
||||
private Long time;
|
||||
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 创建日期
|
||||
*/
|
||||
private Timestamp createTime;
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
package co.yixiang.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.service.LogService;
|
||||
import co.yixiang.utils.PageUtil;
|
||||
import co.yixiang.utils.QueryHelp;
|
||||
import co.yixiang.utils.StringUtils;
|
||||
import co.yixiang.repository.LogRepository;
|
||||
import co.yixiang.service.dto.LogQueryCriteria;
|
||||
import co.yixiang.service.mapper.LogErrorMapper;
|
||||
import co.yixiang.service.mapper.LogSmallMapper;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||
public class LogServiceImpl implements LogService {
|
||||
|
||||
@Autowired
|
||||
private LogRepository logRepository;
|
||||
|
||||
@Autowired
|
||||
private LogErrorMapper logErrorMapper;
|
||||
|
||||
@Autowired
|
||||
private LogSmallMapper logSmallMapper;
|
||||
|
||||
private final String LOGINPATH = "login";
|
||||
|
||||
@Override
|
||||
public Object queryAll(LogQueryCriteria criteria, Pageable pageable){
|
||||
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)),pageable);
|
||||
if ("ERROR".equals(criteria.getLogType())) {
|
||||
return PageUtil.toPage(page.map(logErrorMapper::toDto));
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable) {
|
||||
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)),pageable);
|
||||
return PageUtil.toPage(page.map(logSmallMapper::toDto));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log){
|
||||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
co.yixiang.aop.log.Log aopLog = method.getAnnotation(co.yixiang.aop.log.Log.class);
|
||||
|
||||
// 描述
|
||||
if (log != null) {
|
||||
log.setDescription(aopLog.value());
|
||||
}
|
||||
|
||||
// 方法路径
|
||||
String methodName = joinPoint.getTarget().getClass().getName()+"."+signature.getName()+"()";
|
||||
|
||||
String params = "{";
|
||||
//参数值
|
||||
Object[] argValues = joinPoint.getArgs();
|
||||
//参数名称
|
||||
String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();
|
||||
if(argValues != null){
|
||||
for (int i = 0; i < argValues.length; i++) {
|
||||
params += " " + argNames[i] + ": " + argValues[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 获取IP地址
|
||||
log.setRequestIp(ip);
|
||||
|
||||
if(LOGINPATH.equals(signature.getName())){
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(argValues[0]);
|
||||
username = jsonObject.get("username").toString();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
|
||||
log.setMethod(methodName);
|
||||
log.setUsername(username);
|
||||
log.setParams(params + " }");
|
||||
logRepository.save(log);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object findByErrDetail(Long id) {
|
||||
return Dict.create().set("exception",logRepository.findExceptionById(id));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package co.yixiang.service.mapper;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.mapper.EntityMapper;
|
||||
import co.yixiang.service.dto.LogErrorDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Mapper(componentModel = "spring",uses = {},unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface LogErrorMapper extends EntityMapper<LogErrorDTO, Log> {
|
||||
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
package co.yixiang.service.mapper;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.mapper.EntityMapper;
|
||||
import co.yixiang.service.dto.LogSmallDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2019-5-22
|
||||
*/
|
||||
@Mapper(componentModel = "spring",uses = {},unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface LogSmallMapper extends EntityMapper<LogSmallDTO, Log> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user