14 changed files with 406 additions and 4 deletions
-
15system/src/main/java/com/canvas/web/modules/logging/annotation/Log.java
-
15system/src/main/java/com/canvas/web/modules/logging/aspect/LogAspect.java
-
65system/src/main/java/com/canvas/web/modules/logging/domain/Log.java
-
15system/src/main/java/com/canvas/web/modules/logging/repository/LogRepository.java
-
27system/src/main/java/com/canvas/web/modules/logging/service/LogService.java
-
28system/src/main/java/com/canvas/web/modules/logging/service/dto/LogErrorDTO.java
-
21system/src/main/java/com/canvas/web/modules/logging/service/dto/LogQueryCriteria.java
-
23system/src/main/java/com/canvas/web/modules/logging/service/dto/LogSmallDTO.java
-
135system/src/main/java/com/canvas/web/modules/logging/service/impl/LogServiceImpl.java
-
11system/src/main/java/com/canvas/web/modules/logging/service/mapstruct/LogErrorMapper.java
-
12system/src/main/java/com/canvas/web/modules/logging/service/mapstruct/LogSmallMapper.java
-
24system/src/main/java/com/canvas/web/modules/system/controller/RoleController.java
-
5system/src/main/java/com/canvas/web/modules/system/service/dto/RoleQueryCriteria.java
-
14system/src/main/java/com/canvas/web/modules/system/service/impl/UserServiceImpl.java
@ -0,0 +1,15 @@ |
|||||
|
package com.canvas.web.modules.logging.annotation; |
||||
|
|
||||
|
|
||||
|
import java.lang.annotation.ElementType; |
||||
|
import java.lang.annotation.Retention; |
||||
|
import java.lang.annotation.RetentionPolicy; |
||||
|
import java.lang.annotation.Target; |
||||
|
|
||||
|
@Target(ElementType.METHOD) |
||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||
|
public @interface Log { |
||||
|
|
||||
|
String value() default ""; |
||||
|
|
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.canvas.web.modules.logging.aspect; |
||||
|
|
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.aspectj.lang.annotation.Aspect; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
|
||||
|
@Component |
||||
|
@Aspect |
||||
|
@Slf4j |
||||
|
public class LogAspect { |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
package com.canvas.web.modules.logging.domain; |
||||
|
|
||||
|
import lombok.Getter; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
import lombok.Setter; |
||||
|
import org.hibernate.annotations.CreationTimestamp; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import java.io.Serializable; |
||||
|
import java.sql.Timestamp; |
||||
|
|
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Table(name = "sys_log") |
||||
|
@NoArgsConstructor |
||||
|
public class Log implements Serializable { |
||||
|
|
||||
|
@Id |
||||
|
@Column(name = "id") |
||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY) |
||||
|
private Long id; |
||||
|
|
||||
|
//操作用户 |
||||
|
private String username; |
||||
|
|
||||
|
//描述 |
||||
|
private String description; |
||||
|
|
||||
|
//方法名 |
||||
|
private String method; |
||||
|
|
||||
|
//参数 |
||||
|
private String params; |
||||
|
|
||||
|
//日志类型 |
||||
|
private String logType; |
||||
|
|
||||
|
//请求ip |
||||
|
private String requestIp; |
||||
|
|
||||
|
//地址 |
||||
|
private String address; |
||||
|
|
||||
|
//浏览器 |
||||
|
private String browser; |
||||
|
|
||||
|
//请求耗时 |
||||
|
private Long time; |
||||
|
|
||||
|
//异常详细 |
||||
|
private byte[] exceptionDetail; |
||||
|
|
||||
|
//创建日期 |
||||
|
@CreationTimestamp |
||||
|
private Timestamp createTime; |
||||
|
|
||||
|
|
||||
|
public Log(String logType, Long time) { |
||||
|
this.logType = logType; |
||||
|
this.time = time; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.canvas.web.modules.logging.repository; |
||||
|
|
||||
|
import com.canvas.web.modules.logging.domain.Log; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Modifying; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
|
||||
|
public interface LogRepository extends JpaRepository<Log,Long>, JpaSpecificationExecutor<Log> { |
||||
|
|
||||
|
|
||||
|
@Modifying |
||||
|
@Query(value = "delete from sys_log where log_type = ?1",nativeQuery = true) |
||||
|
void deleteByLogType(String logType); |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package com.canvas.web.modules.logging.service; |
||||
|
|
||||
|
import com.canvas.web.modules.logging.domain.Log; |
||||
|
import com.canvas.web.modules.logging.service.dto.LogQueryCriteria; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.scheduling.annotation.Async; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface LogService { |
||||
|
|
||||
|
Object queryAll(LogQueryCriteria criteria, Pageable pageable); |
||||
|
|
||||
|
List<Log> queryAll(LogQueryCriteria criteria); |
||||
|
|
||||
|
Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable); |
||||
|
|
||||
|
@Async |
||||
|
void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log); |
||||
|
|
||||
|
Object findByErrDetail(Long id); |
||||
|
|
||||
|
void delAllByError(); |
||||
|
|
||||
|
void delAllByInfo(); |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
package com.canvas.web.modules.logging.service.dto; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.sql.Timestamp; |
||||
|
|
||||
|
|
||||
|
@Data |
||||
|
public class LogErrorDTO { |
||||
|
|
||||
|
private Long id; |
||||
|
|
||||
|
private String username; |
||||
|
|
||||
|
private String description; |
||||
|
|
||||
|
private String method; |
||||
|
|
||||
|
private String params; |
||||
|
|
||||
|
private String browser; |
||||
|
|
||||
|
private String requestIp; |
||||
|
|
||||
|
private String address; |
||||
|
|
||||
|
private Timestamp createTime; |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package com.canvas.web.modules.logging.service.dto; |
||||
|
|
||||
|
|
||||
|
import com.canvas.web.annotation.Query; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.sql.Timestamp; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class LogQueryCriteria { |
||||
|
|
||||
|
@Query(blurry = "username,description,address,requestIp,method,params") |
||||
|
private String blurry; |
||||
|
|
||||
|
@Query |
||||
|
private String logType; |
||||
|
|
||||
|
@Query(type = Query.Type.BETWEEN) |
||||
|
private List<Timestamp> createTime; |
||||
|
} |
@ -0,0 +1,23 @@ |
|||||
|
package com.canvas.web.modules.logging.service.dto; |
||||
|
|
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.sql.Timestamp; |
||||
|
|
||||
|
@Data |
||||
|
public class LogSmallDTO implements Serializable { |
||||
|
|
||||
|
private String decription; |
||||
|
|
||||
|
private String requestIp; |
||||
|
|
||||
|
private Long time; |
||||
|
|
||||
|
private String address; |
||||
|
|
||||
|
private String browser; |
||||
|
|
||||
|
private Timestamp createTime; |
||||
|
} |
@ -0,0 +1,135 @@ |
|||||
|
package com.canvas.web.modules.logging.service.impl; |
||||
|
|
||||
|
import cn.hutool.core.lang.Dict; |
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
import com.canvas.web.modules.logging.domain.Log; |
||||
|
import com.canvas.web.modules.logging.repository.LogRepository; |
||||
|
import com.canvas.web.modules.logging.service.LogService; |
||||
|
import com.canvas.web.modules.logging.service.dto.LogQueryCriteria; |
||||
|
import com.canvas.web.modules.logging.service.mapstruct.LogErrorMapper; |
||||
|
import com.canvas.web.modules.logging.service.mapstruct.LogSmallMapper; |
||||
|
import com.canvas.web.utils.*; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.aspectj.lang.reflect.MethodSignature; |
||||
|
import org.springframework.data.domain.Page; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
|
||||
|
import java.lang.reflect.Method; |
||||
|
import java.lang.reflect.Parameter; |
||||
|
import java.util.*; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
public class LogServiceImpl implements LogService { |
||||
|
|
||||
|
private final LogRepository logRepository; |
||||
|
private final LogErrorMapper logErrorMapper; |
||||
|
private final LogSmallMapper logSmallMapper; |
||||
|
|
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public Object queryAll(LogQueryCriteria criteria, Pageable pageable) { |
||||
|
Page<Log> page = logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb)), pageable); |
||||
|
String status = "ERROR"; |
||||
|
if (status.equals(criteria.getLogType())) { |
||||
|
return PageUtil.toPage(page.map(logErrorMapper::toDto)); |
||||
|
} |
||||
|
return page; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public List<Log> queryAll(LogQueryCriteria criteria) { |
||||
|
return logRepository.findAll(((root, criteriaQuery, cb) -> QueryHelp.getPredicate(root, criteria, cb))); |
||||
|
} |
||||
|
|
||||
|
@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 browser, String ip, ProceedingJoinPoint joinPoint, Log log) { |
||||
|
if (log == null) { |
||||
|
throw new IllegalArgumentException("Log 不能为 null!"); |
||||
|
} |
||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
||||
|
Method method = signature.getMethod(); |
||||
|
com.canvas.web.modules.logging.annotation.Log aopLog = method.getAnnotation( com.canvas.web.modules.logging.annotation.Log.class); |
||||
|
|
||||
|
// 方法路径 |
||||
|
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; |
||||
|
|
||||
|
// 描述 |
||||
|
log.setDescription(aopLog.value()); |
||||
|
|
||||
|
log.setRequestIp(ip); |
||||
|
log.setAddress(StringUtils.getCityInfo(log.getRequestIp())); |
||||
|
log.setMethod(methodName); |
||||
|
log.setUsername(username); |
||||
|
log.setParams(getParameter(method, joinPoint.getArgs())); |
||||
|
log.setBrowser(browser); |
||||
|
logRepository.save(log); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 根据方法和传入的参数获取请求参数 |
||||
|
*/ |
||||
|
private String getParameter(Method method, Object[] args) { |
||||
|
List<Object> argList = new ArrayList<>(); |
||||
|
Parameter[] parameters = method.getParameters(); |
||||
|
for (int i = 0; i < parameters.length; i++) { |
||||
|
//将RequestBody注解修饰的参数作为请求参数 |
||||
|
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); |
||||
|
if (requestBody != null) { |
||||
|
argList.add(args[i]); |
||||
|
} |
||||
|
//将RequestParam注解修饰的参数作为请求参数 |
||||
|
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); |
||||
|
if (requestParam != null) { |
||||
|
Map<String, Object> map = new HashMap<>(4); |
||||
|
String key = parameters[i].getName(); |
||||
|
if (!StringUtils.isEmpty(requestParam.value())) { |
||||
|
key = requestParam.value(); |
||||
|
} |
||||
|
map.put(key, args[i]); |
||||
|
argList.add(map); |
||||
|
} |
||||
|
} |
||||
|
if (argList.isEmpty()) { |
||||
|
return ""; |
||||
|
} |
||||
|
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object findByErrDetail(Long id) { |
||||
|
Log log = logRepository.findById(id).orElseGet(Log::new); |
||||
|
ValidationUtil.isNull(log.getId(), "Log", "id", id); |
||||
|
byte[] details = log.getExceptionDetail(); |
||||
|
return Dict.create().set("exception", new String(ObjectUtil.isNotNull(details) ? details : "".getBytes())); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
public void delAllByError() { |
||||
|
logRepository.deleteByLogType("ERROR"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
public void delAllByInfo() { |
||||
|
logRepository.deleteByLogType("INFO"); |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.canvas.web.modules.logging.service.mapstruct; |
||||
|
|
||||
|
import com.canvas.web.base.BaseMapper; |
||||
|
import com.canvas.web.modules.logging.domain.Log; |
||||
|
import com.canvas.web.modules.logging.service.dto.LogErrorDTO; |
||||
|
import org.mapstruct.Mapper; |
||||
|
import org.mapstruct.ReportingPolicy; |
||||
|
|
||||
|
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE) |
||||
|
public interface LogErrorMapper extends BaseMapper<LogErrorDTO, Log> { |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package com.canvas.web.modules.logging.service.mapstruct; |
||||
|
|
||||
|
import com.canvas.web.base.BaseMapper; |
||||
|
import com.canvas.web.modules.logging.domain.Log; |
||||
|
import com.canvas.web.modules.logging.service.dto.LogSmallDTO; |
||||
|
import org.mapstruct.Mapper; |
||||
|
import org.mapstruct.ReportingPolicy; |
||||
|
|
||||
|
|
||||
|
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE) |
||||
|
public interface LogSmallMapper extends BaseMapper<LogSmallDTO, Log> { |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue