1.0版本完成
This commit is contained in:
22
yshop-logging/pom.xml
Normal file
22
yshop-logging/pom.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>yshop</artifactId>
|
||||
<groupId>co.yixiang</groupId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>yshop-logging</artifactId>
|
||||
<name>日志模块</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>co.yixiang</groupId>
|
||||
<artifactId>yshop-common</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
16
yshop-logging/src/main/java/co/yixiang/aop/log/Log.java
Normal file
16
yshop-logging/src/main/java/co/yixiang/aop/log/Log.java
Normal file
@ -0,0 +1,16 @@
|
||||
package co.yixiang.aop.log;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Log {
|
||||
String value() default "";
|
||||
}
|
76
yshop-logging/src/main/java/co/yixiang/aspect/LogAspect.java
Normal file
76
yshop-logging/src/main/java/co/yixiang/aspect/LogAspect.java
Normal file
@ -0,0 +1,76 @@
|
||||
package co.yixiang.aspect;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import co.yixiang.utils.RequestHolder;
|
||||
import co.yixiang.utils.SecurityUtils;
|
||||
import co.yixiang.utils.StringUtils;
|
||||
import co.yixiang.utils.ThrowableUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import co.yixiang.service.LogService;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class LogAspect {
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
private long currentTime = 0L;
|
||||
|
||||
/**
|
||||
* 配置切入点
|
||||
*/
|
||||
@Pointcut("@annotation(co.yixiang.aop.log.Log)")
|
||||
public void logPointcut() {
|
||||
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置环绕通知,使用在方法logPointcut()上注册的切入点
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
*/
|
||||
@Around("logPointcut()")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Object result = null;
|
||||
currentTime = System.currentTimeMillis();
|
||||
result = joinPoint.proceed();
|
||||
Log log = new Log("INFO",System.currentTimeMillis() - currentTime);
|
||||
logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()),joinPoint, log);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置异常通知
|
||||
*
|
||||
* @param joinPoint join point for advice
|
||||
* @param e exception
|
||||
*/
|
||||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
||||
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime);
|
||||
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes());
|
||||
logService.save(getUsername(), StringUtils.getIP(RequestHolder.getHttpServletRequest()), (ProceedingJoinPoint)joinPoint, log);
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
try {
|
||||
return SecurityUtils.getUsername();
|
||||
}catch (Exception e){
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
82
yshop-logging/src/main/java/co/yixiang/domain/Log.java
Normal file
82
yshop-logging/src/main/java/co/yixiang/domain/Log.java
Normal file
@ -0,0 +1,82 @@
|
||||
package co.yixiang.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "log")
|
||||
@NoArgsConstructor
|
||||
public class Log implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 操作用户
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 方法名
|
||||
*/
|
||||
private String method;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
@Column(columnDefinition = "text")
|
||||
private String params;
|
||||
|
||||
/**
|
||||
* 日志类型
|
||||
*/
|
||||
@Column(name = "log_type")
|
||||
private String logType;
|
||||
|
||||
/**
|
||||
* 请求ip
|
||||
*/
|
||||
@Column(name = "request_ip")
|
||||
private String requestIp;
|
||||
|
||||
@Column(name = "address")
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 请求耗时
|
||||
*/
|
||||
private Long time;
|
||||
|
||||
/**
|
||||
* 异常详细
|
||||
*/
|
||||
@Column(name = "exception_detail", columnDefinition = "text")
|
||||
private byte[] exceptionDetail;
|
||||
|
||||
/**
|
||||
* 创建日期
|
||||
*/
|
||||
@CreationTimestamp
|
||||
@Column(name = "create_time")
|
||||
private Timestamp createTime;
|
||||
|
||||
public Log(String logType, Long time) {
|
||||
this.logType = logType;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package co.yixiang.repository;
|
||||
|
||||
import co.yixiang.domain.Log;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@Repository
|
||||
public interface LogRepository extends JpaRepository<Log,Long>, JpaSpecificationExecutor {
|
||||
|
||||
/**
|
||||
* 获取一个时间段的IP记录
|
||||
* @param date1
|
||||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "select count(*) FROM (select request_ip FROM log where create_time between ?1 and ?2 GROUP BY request_ip) as s",nativeQuery = true)
|
||||
Long findIp(String date1, String date2);
|
||||
|
||||
/**
|
||||
* findExceptionById
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "select exception_detail FROM log where id = ?1",nativeQuery = true)
|
||||
String findExceptionById(Long id);
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package co.yixiang.rest;
|
||||
|
||||
import co.yixiang.utils.SecurityUtils;
|
||||
import co.yixiang.service.LogService;
|
||||
import co.yixiang.service.dto.LogQueryCriteria;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-24
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("api")
|
||||
public class LogController {
|
||||
|
||||
@Autowired
|
||||
private LogService logService;
|
||||
|
||||
@GetMapping(value = "/logs")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
public ResponseEntity getLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("INFO");
|
||||
return new ResponseEntity(logService.queryAll(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/logs/user")
|
||||
public ResponseEntity getUserLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("INFO");
|
||||
criteria.setBlurry(SecurityUtils.getUsername());
|
||||
return new ResponseEntity(logService.queryAllByUser(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/logs/error")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
public ResponseEntity getErrorLogs(LogQueryCriteria criteria, Pageable pageable){
|
||||
criteria.setLogType("ERROR");
|
||||
return new ResponseEntity(logService.queryAll(criteria,pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/logs/error/{id}")
|
||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||
public ResponseEntity getErrorLogs(@PathVariable Long id){
|
||||
return new ResponseEntity(logService.findByErrDetail(id), HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -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