23 changed files with 730 additions and 75 deletions
-
18common/src/main/java/com/storeroom/utils/ThrowableUtil.java
-
29logging/pom.xml
-
13logging/src/main/java/com/storeroom/annotation/Log.java
-
82logging/src/main/java/com/storeroom/aspect/LogAspect.java
-
93logging/src/main/java/com/storeroom/controller/LogController.java
-
62logging/src/main/java/com/storeroom/domain/Log.java
-
17logging/src/main/java/com/storeroom/repository/LogRepository.java
-
73logging/src/main/java/com/storeroom/service/LogService.java
-
29logging/src/main/java/com/storeroom/service/dto/LogErrorDTO.java
-
20logging/src/main/java/com/storeroom/service/dto/LogQueryCriteria.java
-
22logging/src/main/java/com/storeroom/service/dto/LogSmallDTO.java
-
151logging/src/main/java/com/storeroom/service/impl/LogServiceImpl.java
-
11logging/src/main/java/com/storeroom/service/mapsturct/LogErrorMapper.java
-
11logging/src/main/java/com/storeroom/service/mapsturct/LogSmallMapper.java
-
14pom.xml
-
7system/pom.xml
-
9system/src/main/java/com/storeroom/AppRun.java
-
19system/src/main/java/com/storeroom/modules/system/controller/DeptController.java
-
19system/src/main/java/com/storeroom/modules/system/controller/DictController.java
-
13system/src/main/java/com/storeroom/modules/system/controller/DictDetailController.java
-
23system/src/main/java/com/storeroom/modules/system/controller/MenuController.java
-
23system/src/main/java/com/storeroom/modules/system/controller/RoleController.java
-
33system/src/main/java/com/storeroom/modules/system/controller/UserController.java
@ -0,0 +1,18 @@ |
|||||
|
package com.storeroom.utils; |
||||
|
|
||||
|
import java.io.PrintWriter; |
||||
|
import java.io.StringWriter; |
||||
|
|
||||
|
public class ThrowableUtil { |
||||
|
|
||||
|
/** |
||||
|
* 获取堆栈信息 |
||||
|
*/ |
||||
|
public static String getStackTrace(Throwable throwable){ |
||||
|
StringWriter sw = new StringWriter(); |
||||
|
try (PrintWriter pw = new PrintWriter(sw)) { |
||||
|
throwable.printStackTrace(pw); |
||||
|
return sw.toString(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
<?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>yxk_StoreroomSystem</artifactId> |
||||
|
<groupId>com.storeroom</groupId> |
||||
|
<version>1.0</version> |
||||
|
</parent> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<artifactId>logging</artifactId> |
||||
|
<name>日志模块</name> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>17</maven.compiler.source> |
||||
|
<maven.compiler.target>17</maven.compiler.target> |
||||
|
</properties> |
||||
|
|
||||
|
<!--添加公共模块依赖项--> |
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>com.storeroom</groupId> |
||||
|
<artifactId>common</artifactId> |
||||
|
<version>1.0</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,13 @@ |
|||||
|
package com.storeroom.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,82 @@ |
|||||
|
package com.storeroom.aspect; |
||||
|
|
||||
|
|
||||
|
import com.storeroom.domain.Log; |
||||
|
import com.storeroom.service.LogService; |
||||
|
import com.storeroom.utils.RequestHolder; |
||||
|
import com.storeroom.utils.SecurityUtils; |
||||
|
import com.storeroom.utils.StringUtils; |
||||
|
import com.storeroom.utils.ThrowableUtil; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
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.stereotype.Component; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletRequest; |
||||
|
|
||||
|
@Component |
||||
|
@Aspect |
||||
|
@Slf4j |
||||
|
public class LogAspect { |
||||
|
|
||||
|
|
||||
|
private final LogService logService; |
||||
|
|
||||
|
ThreadLocal<Long> currentTime = new ThreadLocal<>(); |
||||
|
|
||||
|
public LogAspect(LogService logService) { |
||||
|
this.logService = logService; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 配置切入点 |
||||
|
*/ |
||||
|
@Pointcut("@annotation(com.storeroom.annotation.Log)") |
||||
|
public void logPointcut() { |
||||
|
// 该方法无方法体,主要为了让同类中其他方法使用此切入点 |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 配置环绕通知,使用在方法logPointcut()上注册的切入点 |
||||
|
* |
||||
|
* @param joinPoint join point for advice |
||||
|
*/ |
||||
|
@Around("logPointcut()") |
||||
|
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { |
||||
|
Object result; |
||||
|
currentTime.set(System.currentTimeMillis()); |
||||
|
result = joinPoint.proceed(); |
||||
|
Log log = new Log("INFO",System.currentTimeMillis() - currentTime.get()); |
||||
|
currentTime.remove(); |
||||
|
HttpServletRequest request = RequestHolder.getHttpServletRequest(); |
||||
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request),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.get()); |
||||
|
currentTime.remove(); |
||||
|
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes()); |
||||
|
HttpServletRequest request = RequestHolder.getHttpServletRequest(); |
||||
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, log); |
||||
|
} |
||||
|
|
||||
|
public String getUsername() { |
||||
|
try { |
||||
|
return SecurityUtils.getCurrentUsername(); |
||||
|
}catch (Exception e){ |
||||
|
return ""; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,93 @@ |
|||||
|
package com.storeroom.controller; |
||||
|
|
||||
|
|
||||
|
import com.storeroom.annotation.Log; |
||||
|
import com.storeroom.service.LogService; |
||||
|
import com.storeroom.service.dto.LogQueryCriteria; |
||||
|
import com.storeroom.utils.SecurityUtils; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
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.*; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
|
||||
|
@RestController |
||||
|
@RequiredArgsConstructor |
||||
|
@RequestMapping("/api/logs") |
||||
|
@Api(tags = "系统:日志管理") |
||||
|
public class LogController { |
||||
|
|
||||
|
private final LogService logService; |
||||
|
|
||||
|
|
||||
|
@Log("导出数据") |
||||
|
@ApiOperation("导出数据") |
||||
|
@GetMapping(value = "/download") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public void exportLog(HttpServletResponse response, LogQueryCriteria criteria) throws IOException { |
||||
|
criteria.setLogType("INFO"); |
||||
|
logService.download(logService.queryAll(criteria), response); |
||||
|
} |
||||
|
|
||||
|
@Log("导出错误数据") |
||||
|
@ApiOperation("导出错误数据") |
||||
|
@GetMapping(value = "/error/download") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public void exportErrorLog(HttpServletResponse response, LogQueryCriteria criteria) throws IOException { |
||||
|
criteria.setLogType("ERROR"); |
||||
|
logService.download(logService.queryAll(criteria), response); |
||||
|
} |
||||
|
@GetMapping |
||||
|
@ApiOperation("日志查询") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public ResponseEntity<Object> queryLog(LogQueryCriteria criteria, Pageable pageable){ |
||||
|
criteria.setLogType("INFO"); |
||||
|
return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK); |
||||
|
} |
||||
|
|
||||
|
@GetMapping(value = "/user") |
||||
|
@ApiOperation("用户日志查询") |
||||
|
public ResponseEntity<Object> queryUserLog(LogQueryCriteria criteria, Pageable pageable){ |
||||
|
criteria.setLogType("INFO"); |
||||
|
criteria.setBlurry(SecurityUtils.getCurrentUsername()); |
||||
|
return new ResponseEntity<>(logService.queryAllByUser(criteria,pageable), HttpStatus.OK); |
||||
|
} |
||||
|
|
||||
|
@GetMapping(value = "/error") |
||||
|
@ApiOperation("错误日志查询") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public ResponseEntity<Object> queryErrorLog(LogQueryCriteria criteria, Pageable pageable){ |
||||
|
criteria.setLogType("ERROR"); |
||||
|
return new ResponseEntity<>(logService.queryAll(criteria,pageable), HttpStatus.OK); |
||||
|
} |
||||
|
|
||||
|
@GetMapping(value = "/error/{id}") |
||||
|
@ApiOperation("日志异常详情查询") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public ResponseEntity<Object> queryErrorLogDetail(@PathVariable Long id){ |
||||
|
return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK); |
||||
|
} |
||||
|
@DeleteMapping(value = "/del/error") |
||||
|
@Log("删除所有ERROR日志") |
||||
|
@ApiOperation("删除所有ERROR日志") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public ResponseEntity<Object> delAllErrorLog(){ |
||||
|
logService.delAllByError(); |
||||
|
return new ResponseEntity<>(HttpStatus.OK); |
||||
|
} |
||||
|
|
||||
|
@DeleteMapping(value = "/del/info") |
||||
|
@Log("删除所有INFO日志") |
||||
|
@ApiOperation("删除所有INFO日志") |
||||
|
@PreAuthorize("@ys.check()") |
||||
|
public ResponseEntity<Object> delAllInfoLog(){ |
||||
|
logService.delAllByInfo(); |
||||
|
return new ResponseEntity<>(HttpStatus.OK); |
||||
|
} |
||||
|
} |
@ -0,0 +1,62 @@ |
|||||
|
package com.storeroom.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; |
||||
|
|
||||
|
@Entity |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Table(name = "sys_log") |
||||
|
@NoArgsConstructor |
||||
|
public class Log implements Serializable { |
||||
|
|
||||
|
@Id |
||||
|
@Column(name = "log_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,17 @@ |
|||||
|
package com.storeroom.repository; |
||||
|
|
||||
|
|
||||
|
import com.storeroom.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; |
||||
|
import org.springframework.stereotype.Repository; |
||||
|
|
||||
|
@Repository |
||||
|
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,73 @@ |
|||||
|
package com.storeroom.service; |
||||
|
|
||||
|
import com.storeroom.domain.Log; |
||||
|
import com.storeroom.service.dto.LogQueryCriteria; |
||||
|
import org.aspectj.lang.ProceedingJoinPoint; |
||||
|
import org.springframework.data.domain.Pageable; |
||||
|
import org.springframework.scheduling.annotation.Async; |
||||
|
|
||||
|
import javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface LogService { |
||||
|
|
||||
|
/** |
||||
|
* 分页查询 |
||||
|
* @param criteria 查询条件 |
||||
|
* @param pageable 分页参数 |
||||
|
* @return / |
||||
|
*/ |
||||
|
Object queryAll(LogQueryCriteria criteria, Pageable pageable); |
||||
|
|
||||
|
/** |
||||
|
* 查询全部数据 |
||||
|
* @param criteria 查询条件 |
||||
|
* @return / |
||||
|
*/ |
||||
|
List<Log> queryAll(LogQueryCriteria criteria); |
||||
|
|
||||
|
/** |
||||
|
* 查询用户日志 |
||||
|
* @param criteria 查询条件 |
||||
|
* @param pageable 分页参数 |
||||
|
* @return - |
||||
|
*/ |
||||
|
Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable); |
||||
|
|
||||
|
/** |
||||
|
* 保存日志数据 |
||||
|
* @param username 用户 |
||||
|
* @param browser 浏览器 |
||||
|
* @param ip 请求IP |
||||
|
* @param joinPoint / |
||||
|
* @param log 日志实体 |
||||
|
*/ |
||||
|
@Async |
||||
|
void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log); |
||||
|
|
||||
|
/** |
||||
|
* 查询异常详情 |
||||
|
* @param id 日志ID |
||||
|
* @return Object |
||||
|
*/ |
||||
|
Object findByErrDetail(Long id); |
||||
|
|
||||
|
/** |
||||
|
* 导出日志 |
||||
|
* @param logs 待导出的数据 |
||||
|
* @param response / |
||||
|
* @throws IOException / |
||||
|
*/ |
||||
|
void download(List<Log> logs, HttpServletResponse response) throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 删除所有错误日志 |
||||
|
*/ |
||||
|
void delAllByError(); |
||||
|
|
||||
|
/** |
||||
|
* 删除所有INFO日志 |
||||
|
*/ |
||||
|
void delAllByInfo(); |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package com.storeroom.service.dto; |
||||
|
|
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.sql.Timestamp; |
||||
|
|
||||
|
@Data |
||||
|
public class LogErrorDTO implements Serializable { |
||||
|
|
||||
|
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,20 @@ |
|||||
|
package com.storeroom.service.dto; |
||||
|
|
||||
|
import com.storeroom.annotaion.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,22 @@ |
|||||
|
package com.storeroom.service.dto; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.sql.Timestamp; |
||||
|
|
||||
|
@Data |
||||
|
public class LogSmallDTO implements Serializable { |
||||
|
|
||||
|
private String description; |
||||
|
|
||||
|
private String requestIp; |
||||
|
|
||||
|
private Long time; |
||||
|
|
||||
|
private String address; |
||||
|
|
||||
|
private String browser; |
||||
|
|
||||
|
private Timestamp createTime; |
||||
|
} |
@ -0,0 +1,151 @@ |
|||||
|
package com.storeroom.service.impl; |
||||
|
|
||||
|
import cn.hutool.core.lang.Dict; |
||||
|
import cn.hutool.core.util.ObjectUtil; |
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
import com.storeroom.domain.Log; |
||||
|
import com.storeroom.repository.LogRepository; |
||||
|
import com.storeroom.service.LogService; |
||||
|
import com.storeroom.service.dto.LogQueryCriteria; |
||||
|
import com.storeroom.service.mapsturct.LogErrorMapper; |
||||
|
import com.storeroom.service.mapsturct.LogSmallMapper; |
||||
|
import com.storeroom.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 javax.servlet.http.HttpServletResponse; |
||||
|
import java.io.IOException; |
||||
|
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.storeroom.annotation.Log aopLog = method.getAnnotation(com.storeroom.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 |
||||
|
public void download(List<Log> logs, HttpServletResponse response) throws IOException { |
||||
|
List<Map<String, Object>> list = new ArrayList<>(); |
||||
|
for (Log log : logs) { |
||||
|
Map<String, Object> map = new LinkedHashMap<>(); |
||||
|
map.put("用户名", log.getUsername()); |
||||
|
map.put("IP", log.getRequestIp()); |
||||
|
map.put("IP来源", log.getAddress()); |
||||
|
map.put("描述", log.getDescription()); |
||||
|
map.put("浏览器", log.getBrowser()); |
||||
|
map.put("请求耗时/毫秒", log.getTime()); |
||||
|
map.put("异常详情", new String(ObjectUtil.isNotNull(log.getExceptionDetail()) ? log.getExceptionDetail() : "".getBytes())); |
||||
|
map.put("创建日期", log.getCreateTime()); |
||||
|
list.add(map); |
||||
|
} |
||||
|
FileUtil.downloadExcel(list, response); |
||||
|
} |
||||
|
|
||||
|
@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.storeroom.service.mapsturct; |
||||
|
|
||||
|
import com.storeroom.base.BaseMapper; |
||||
|
import com.storeroom.domain.Log; |
||||
|
import com.storeroom.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,11 @@ |
|||||
|
package com.storeroom.service.mapsturct; |
||||
|
|
||||
|
import com.storeroom.base.BaseMapper; |
||||
|
import com.storeroom.domain.Log; |
||||
|
import com.storeroom.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