feat(core): 增加系统日志功能
- 在 ConfigController 和 UserController 中添加日志注解 - 新增 LogModuleEnum.EXCEPTION 枚举值 - 在 Log 注解中添加 params 和 result 属性 - 实现日志切面,记录请求参数和响应结果 - 优化日志保存逻辑,支持异常日志记录
This commit is contained in:
@@ -14,7 +14,7 @@ import lombok.Getter;
|
|||||||
@Getter
|
@Getter
|
||||||
public enum LogModuleEnum {
|
public enum LogModuleEnum {
|
||||||
|
|
||||||
|
EXCEPTION("异常"),
|
||||||
LOGIN("登录"),
|
LOGIN("登录"),
|
||||||
USER("用户"),
|
USER("用户"),
|
||||||
DEPT("部门"),
|
DEPT("部门"),
|
||||||
|
|||||||
@@ -15,9 +15,35 @@ import java.lang.annotation.*;
|
|||||||
@Documented
|
@Documented
|
||||||
public @interface Log {
|
public @interface Log {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志描述
|
||||||
|
*
|
||||||
|
* @return 日志描述
|
||||||
|
*/
|
||||||
String value() default "";
|
String value() default "";
|
||||||
|
|
||||||
LogModuleEnum module() ;
|
/**
|
||||||
|
* 日志模块
|
||||||
|
*
|
||||||
|
* @return 日志模块
|
||||||
|
*/
|
||||||
|
|
||||||
|
LogModuleEnum module();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否记录请求参数
|
||||||
|
*
|
||||||
|
* @return 是否记录请求参数
|
||||||
|
*/
|
||||||
|
boolean params() default true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否记录响应结果
|
||||||
|
* <br/>
|
||||||
|
* 请求参数默认不记录,避免日志过大
|
||||||
|
* @return 是否记录响应结果
|
||||||
|
*/
|
||||||
|
boolean result() default false;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,16 +8,19 @@ import cn.hutool.http.useragent.UserAgentUtil;
|
|||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.aliyun.oss.HttpMethod;
|
import com.aliyun.oss.HttpMethod;
|
||||||
import com.youlai.boot.common.constant.SecurityConstants;
|
import com.youlai.boot.common.constant.SecurityConstants;
|
||||||
|
import com.youlai.boot.common.enums.LogModuleEnum;
|
||||||
import com.youlai.boot.common.util.IPUtils;
|
import com.youlai.boot.common.util.IPUtils;
|
||||||
import com.youlai.boot.core.security.util.SecurityUtils;
|
import com.youlai.boot.core.security.util.SecurityUtils;
|
||||||
import com.youlai.boot.system.model.entity.Log;
|
import com.youlai.boot.system.model.entity.Log;
|
||||||
import com.youlai.boot.system.service.LogService;
|
import com.youlai.boot.system.service.LogService;
|
||||||
|
import groovyjarjarpicocli.CommandLine;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.AfterReturning;
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Pointcut;
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -55,6 +58,25 @@ public class LogAspect {
|
|||||||
*/
|
*/
|
||||||
@AfterReturning(pointcut = "logPointcut() && @annotation(logAnnotation)", returning = "jsonResult")
|
@AfterReturning(pointcut = "logPointcut() && @annotation(logAnnotation)", returning = "jsonResult")
|
||||||
public void doAfterReturning(JoinPoint joinPoint, com.youlai.boot.core.annotation.Log logAnnotation, Object jsonResult) {
|
public void doAfterReturning(JoinPoint joinPoint, com.youlai.boot.core.annotation.Log logAnnotation, Object jsonResult) {
|
||||||
|
this.saveLog(joinPoint, null, jsonResult, logAnnotation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拦截异常操作
|
||||||
|
*
|
||||||
|
* @param joinPoint 切点
|
||||||
|
* @param e 异常
|
||||||
|
*/
|
||||||
|
@AfterThrowing(value = "logPointcut()", throwing = "e")
|
||||||
|
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
|
||||||
|
this.saveLog(joinPoint, e, null,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保持日志
|
||||||
|
*/
|
||||||
|
private void saveLog(final JoinPoint joinPoint, final Exception e, Object jsonResult, com.youlai.boot.core.annotation.Log logAnnotation) {
|
||||||
String requestURI = request.getRequestURI();
|
String requestURI = request.getRequestURI();
|
||||||
|
|
||||||
Long userId = null;
|
Long userId = null;
|
||||||
@@ -69,8 +91,24 @@ public class LogAspect {
|
|||||||
|
|
||||||
// 创建日志记录
|
// 创建日志记录
|
||||||
Log log = new Log();
|
Log log = new Log();
|
||||||
|
if (logAnnotation == null && e != null) {
|
||||||
|
log.setModule(LogModuleEnum.EXCEPTION);
|
||||||
|
log.setContent("系统发生异常");
|
||||||
|
this.setRequestParameters(joinPoint, log);
|
||||||
|
log.setResponseContent(JSONUtil.toJsonStr(e.getStackTrace()));
|
||||||
|
}else{
|
||||||
log.setModule(logAnnotation.module());
|
log.setModule(logAnnotation.module());
|
||||||
log.setContent(logAnnotation.value());
|
log.setContent(logAnnotation.value());
|
||||||
|
// 请求参数
|
||||||
|
if (logAnnotation.params()) {
|
||||||
|
this.setRequestParameters(joinPoint, log);
|
||||||
|
}
|
||||||
|
// 响应结果
|
||||||
|
if (logAnnotation.result() && jsonResult != null) {
|
||||||
|
log.setResponseContent(JSONUtil.toJsonStr(jsonResult));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.setRequestUri(requestURI);
|
log.setRequestUri(requestURI);
|
||||||
// 登录方法需要在登录成功后获取用户ID
|
// 登录方法需要在登录成功后获取用户ID
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
@@ -90,8 +128,7 @@ public class LogAspect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.setRequestParameters(joinPoint, log);
|
|
||||||
log.setResponseContent(JSONUtil.toJsonStr(jsonResult));
|
|
||||||
log.setExecutionTime(executionTime);
|
log.setExecutionTime(executionTime);
|
||||||
// 获取浏览器和终端系统信息
|
// 获取浏览器和终端系统信息
|
||||||
String userAgentString = request.getHeader("User-Agent");
|
String userAgentString = request.getHeader("User-Agent");
|
||||||
@@ -103,7 +140,6 @@ public class LogAspect {
|
|||||||
log.setBrowserVersion(userAgent.getBrowser().getVersion(userAgentString));
|
log.setBrowserVersion(userAgent.getBrowser().getVersion(userAgentString));
|
||||||
// 保存日志到数据库
|
// 保存日志到数据库
|
||||||
logService.save(log);
|
logService.save(log);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ public class ConfigController {
|
|||||||
@Operation(summary = "系统配置分页列表")
|
@Operation(summary = "系统配置分页列表")
|
||||||
@GetMapping("/page")
|
@GetMapping("/page")
|
||||||
@PreAuthorize("@ss.hasPerm('sys:config:query')")
|
@PreAuthorize("@ss.hasPerm('sys:config:query')")
|
||||||
|
@Log( value = "系统配置分页列表",module = LogModuleEnum.SETTING)
|
||||||
public PageResult<ConfigVO> page(@ParameterObject ConfigPageQuery configPageQuery) {
|
public PageResult<ConfigVO> page(@ParameterObject ConfigPageQuery configPageQuery) {
|
||||||
IPage<ConfigVO> result = configService.page(configPageQuery);
|
IPage<ConfigVO> result = configService.page(configPageQuery);
|
||||||
return PageResult.success(result);
|
return PageResult.success(result);
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ public class UserController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
@PreAuthorize("@ss.hasPerm('sys:user:add')")
|
@PreAuthorize("@ss.hasPerm('sys:user:add')")
|
||||||
@RepeatSubmit
|
@RepeatSubmit
|
||||||
|
@Log(value = "新增用户", module = LogModuleEnum.USER)
|
||||||
public Result<?> saveUser(
|
public Result<?> saveUser(
|
||||||
@RequestBody @Valid UserForm userForm
|
@RequestBody @Valid UserForm userForm
|
||||||
) {
|
) {
|
||||||
@@ -79,6 +80,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "用户表单数据")
|
@Operation(summary = "用户表单数据")
|
||||||
@GetMapping("/{userId}/form")
|
@GetMapping("/{userId}/form")
|
||||||
|
@Log(value = "用户表单数据", module = LogModuleEnum.USER)
|
||||||
public Result<UserForm> getUserForm(
|
public Result<UserForm> getUserForm(
|
||||||
@Parameter(description = "用户ID") @PathVariable Long userId
|
@Parameter(description = "用户ID") @PathVariable Long userId
|
||||||
) {
|
) {
|
||||||
@@ -89,6 +91,7 @@ public class UserController {
|
|||||||
@Operation(summary = "修改用户")
|
@Operation(summary = "修改用户")
|
||||||
@PutMapping(value = "/{userId}")
|
@PutMapping(value = "/{userId}")
|
||||||
@PreAuthorize("@ss.hasPerm('sys:user:edit')")
|
@PreAuthorize("@ss.hasPerm('sys:user:edit')")
|
||||||
|
@Log(value = "修改用户", module = LogModuleEnum.USER)
|
||||||
public Result<Void> updateUser(
|
public Result<Void> updateUser(
|
||||||
@Parameter(description = "用户ID") @PathVariable Long userId,
|
@Parameter(description = "用户ID") @PathVariable Long userId,
|
||||||
@RequestBody @Valid UserForm userForm
|
@RequestBody @Valid UserForm userForm
|
||||||
@@ -100,6 +103,7 @@ public class UserController {
|
|||||||
@Operation(summary = "删除用户")
|
@Operation(summary = "删除用户")
|
||||||
@DeleteMapping("/{ids}")
|
@DeleteMapping("/{ids}")
|
||||||
@PreAuthorize("@ss.hasPerm('sys:user:delete')")
|
@PreAuthorize("@ss.hasPerm('sys:user:delete')")
|
||||||
|
@Log(value = "删除用户", module = LogModuleEnum.USER)
|
||||||
public Result<Void> deleteUsers(
|
public Result<Void> deleteUsers(
|
||||||
@Parameter(description = "用户ID,多个以英文逗号(,)分割") @PathVariable String ids
|
@Parameter(description = "用户ID,多个以英文逗号(,)分割") @PathVariable String ids
|
||||||
) {
|
) {
|
||||||
@@ -109,6 +113,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "修改用户状态")
|
@Operation(summary = "修改用户状态")
|
||||||
@PatchMapping(value = "/{userId}/status")
|
@PatchMapping(value = "/{userId}/status")
|
||||||
|
@Log(value = "修改用户状态", module = LogModuleEnum.USER)
|
||||||
public Result<Void> updateUserStatus(
|
public Result<Void> updateUserStatus(
|
||||||
@Parameter(description = "用户ID") @PathVariable Long userId,
|
@Parameter(description = "用户ID") @PathVariable Long userId,
|
||||||
@Parameter(description = "用户状态(1:启用;0:禁用)") @RequestParam Integer status
|
@Parameter(description = "用户状态(1:启用;0:禁用)") @RequestParam Integer status
|
||||||
@@ -122,6 +127,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "获取当前登录用户信息")
|
@Operation(summary = "获取当前登录用户信息")
|
||||||
@GetMapping("/me")
|
@GetMapping("/me")
|
||||||
|
@Log(value = "获取当前登录用户信息", module = LogModuleEnum.USER)
|
||||||
public Result<UserInfoVO> getCurrentUserInfo() {
|
public Result<UserInfoVO> getCurrentUserInfo() {
|
||||||
UserInfoVO userInfoVO = userService.getCurrentUserInfo();
|
UserInfoVO userInfoVO = userService.getCurrentUserInfo();
|
||||||
return Result.success(userInfoVO);
|
return Result.success(userInfoVO);
|
||||||
@@ -129,6 +135,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "用户导入模板下载")
|
@Operation(summary = "用户导入模板下载")
|
||||||
@GetMapping("/template")
|
@GetMapping("/template")
|
||||||
|
@Log(value = "用户导入模板下载", module = LogModuleEnum.USER)
|
||||||
public void downloadTemplate(HttpServletResponse response) throws IOException {
|
public void downloadTemplate(HttpServletResponse response) throws IOException {
|
||||||
String fileName = "用户导入模板.xlsx";
|
String fileName = "用户导入模板.xlsx";
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||||
@@ -145,6 +152,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "导入用户")
|
@Operation(summary = "导入用户")
|
||||||
@PostMapping("/import")
|
@PostMapping("/import")
|
||||||
|
@Log(value = "导入用户", module = LogModuleEnum.USER)
|
||||||
public Result<String> importUsers(MultipartFile file) throws IOException {
|
public Result<String> importUsers(MultipartFile file) throws IOException {
|
||||||
UserImportListener listener = new UserImportListener();
|
UserImportListener listener = new UserImportListener();
|
||||||
String msg = ExcelUtils.importExcel(file.getInputStream(), UserImportDTO.class, listener);
|
String msg = ExcelUtils.importExcel(file.getInputStream(), UserImportDTO.class, listener);
|
||||||
@@ -153,6 +161,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "导出用户")
|
@Operation(summary = "导出用户")
|
||||||
@GetMapping("/export")
|
@GetMapping("/export")
|
||||||
|
@Log(value = "导出用户", module = LogModuleEnum.USER)
|
||||||
public void exportUsers(UserPageQuery queryParams, HttpServletResponse response) throws IOException {
|
public void exportUsers(UserPageQuery queryParams, HttpServletResponse response) throws IOException {
|
||||||
String fileName = "用户列表.xlsx";
|
String fileName = "用户列表.xlsx";
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||||
@@ -165,6 +174,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "获取个人中心用户信息")
|
@Operation(summary = "获取个人中心用户信息")
|
||||||
@GetMapping("/profile")
|
@GetMapping("/profile")
|
||||||
|
@Log(value = "获取个人中心用户信息", module = LogModuleEnum.USER)
|
||||||
public Result<UserProfileVO> getUserProfile() {
|
public Result<UserProfileVO> getUserProfile() {
|
||||||
Long userId = SecurityUtils.getUserId();
|
Long userId = SecurityUtils.getUserId();
|
||||||
UserProfileVO userProfile = userService.getUserProfile(userId);
|
UserProfileVO userProfile = userService.getUserProfile(userId);
|
||||||
@@ -173,6 +183,7 @@ public class UserController {
|
|||||||
|
|
||||||
@Operation(summary = "个人中心修改用户信息")
|
@Operation(summary = "个人中心修改用户信息")
|
||||||
@PutMapping("/profile")
|
@PutMapping("/profile")
|
||||||
|
@Log(value = "个人中心修改用户信息", module = LogModuleEnum.USER)
|
||||||
public Result<?> updateUserProfile(@RequestBody UserProfileForm formData) {
|
public Result<?> updateUserProfile(@RequestBody UserProfileForm formData) {
|
||||||
boolean result = userService.updateUserProfile(formData);
|
boolean result = userService.updateUserProfile(formData);
|
||||||
return Result.judge(result);
|
return Result.judge(result);
|
||||||
|
|||||||
Reference in New Issue
Block a user