Skip to content

Commit

Permalink
feat: 新增[系统监控:操作日志]
Browse files Browse the repository at this point in the history
  • Loading branch information
Forever2ha committed Dec 10, 2022
1 parent 8f1faf5 commit 3c0b2e5
Show file tree
Hide file tree
Showing 26 changed files with 1,135 additions and 72 deletions.
546 changes: 474 additions & 72 deletions doc/sql/erha-admin.sql

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fun.yizhierha.common.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 "";
}
4 changes: 4 additions & 0 deletions erha-admin-monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
<artifactId>erha-admin-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fun.yizhierha.monitor.aspect;

import fun.yizhierha.common.utils.SecurityUtils;
import fun.yizhierha.common.utils.StringUtils;
import fun.yizhierha.common.utils.ValidUtils;
import fun.yizhierha.monitor.domain.SysLog;
import fun.yizhierha.monitor.service.SysLogService;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
* @author Zheng Jie
* @date 2018-11-24
*/
@Component
@Aspect
@Slf4j
public class LogAspect {

private final SysLogService sysLogService;

ThreadLocal<Long> currentTime = new ThreadLocal<>();

public LogAspect(SysLogService sysLogService) {
this.sysLogService = sysLogService;
}

/**
* 配置切入点
*/
@Pointcut("@annotation(fun.yizhierha.common.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();
SysLog log = new SysLog();
log.setLogType("INFO");
log.setTime(System.currentTimeMillis() - currentTime.get());
currentTime.remove();
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
sysLogService.save(getUsername(), StringUtils.getBrowserByRequest(request), StringUtils.getIpByRequest(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) {
SysLog log = new SysLog();
log.setLogType("ERROR");
log.setTime(System.currentTimeMillis() - currentTime.get());
currentTime.remove();
log.setExceptionDetail(ValidUtils.getStackTrace(e));
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
sysLogService.save(getUsername(), StringUtils.getBrowserByRequest(request), StringUtils.getIpByRequest(request), (ProceedingJoinPoint)joinPoint, log);
}

public String getUsername() {
try {
return SecurityUtils.getCurrentUser().getUsername();
}catch (Exception e){
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fun.yizhierha.monitor.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.ValidUtils;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class OnlineUserController{
private final OnlineUserService onlineUserService;

@ApiOperation("获取在线用户")
@Log("获取在线用户")
@GetMapping
@PreAuthorize("@eh.check('onlineUser:list')")
public R<PageUtils<OnlineUserDto>> list(RetrieveOnlineUserVo retrieveOnlineUserVo, Query.PageVo pageVo){
Expand All @@ -39,6 +41,7 @@ public R<PageUtils<OnlineUserDto>> list(RetrieveOnlineUserVo retrieveOnlineUserV
}

@ApiOperation("删除在线用户")
@Log("删除在线用户")
@DeleteMapping
@PreAuthorize("@eh.check('onlineUser:del')")
public R delJob(@RequestBody Set<String> usernames){
Expand All @@ -47,6 +50,7 @@ public R delJob(@RequestBody Set<String> usernames){
}

@ApiOperation("导出数据")
@Log("导出数据")
@GetMapping("/download")
@PreAuthorize("@eh.check('onlineUser:list')")
public void download(HttpServletResponse response){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fun.yizhierha.monitor.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.utils.*;
import fun.yizhierha.monitor.domain.SysLog;
import fun.yizhierha.monitor.domain.vo.RetrieveSysLogVo;

import fun.yizhierha.monitor.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/** generated by EH-Admin
* @author 二哈
* @date Fri Dec 09 20:47:12 CST 2022
**/
@Api(tags = "系统监控:操作日志")
@RestController
@RequestMapping("/api/monitor/log")
@RequiredArgsConstructor
public class SysLogController{

private final SysLogService sysLogService;

@ApiOperation("获取操作日志")
@GetMapping
@PreAuthorize("@eh.check('sysLog:list')")
public R<PageUtils<SysLog>> list(RetrieveSysLogVo retrieveSysLogVo, Query.PageVo pageVo){
PageUtils<SysLog> res = sysLogService.list(retrieveSysLogVo,pageVo);
return R.<PageUtils<SysLog>>ok().setData(res);
}

@ApiOperation("删除操作日志")
@Log("删除操作日志")
@DeleteMapping
@PreAuthorize("@eh.check('sysLog:del')")
public R del(@RequestBody Set<Long> ids){
sysLogService.remove(ids);
return R.ok();
}

@ApiOperation("导出数据")
@Log("导出数据")
@GetMapping("/download")
@PreAuthorize("@eh.check('sysLog:list')")
public void download(HttpServletResponse response){
sysLogService.download(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fun.yizhierha.monitor.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import fun.yizhierha.common.utils.file.ExcelExport;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import lombok.Data;

import java.sql.Timestamp;

/** generated by EH-Admin
* @author 二哈
* @date Fri Dec 09 20:47:12 CST 2022
**/
@ApiModel(value = "操作日志")
@Data
@TableName(value = "sys_log")
public class SysLog implements Serializable{

@TableId(value = "log_id", type = IdType.AUTO)
@ApiModelProperty(value = "ID")
@ExcelExport("id")
private Long id;

@TableField(value = "description")
@ApiModelProperty(value = "描述")
@ExcelExport("描述")
private String description;

@TableField(value = "log_type")
@ApiModelProperty(value = "日志类型")
@ExcelExport("日志类型")
private String logType;

@TableField(value = "method")
@ApiModelProperty(value = "执行方法")
@ExcelExport("执行方法")
private String method;

@TableField(value = "params")
@ApiModelProperty(value = "参数")
@ExcelExport("参数")
private String params;

@TableField(value = "request_ip")
@ApiModelProperty(value = "IP来源")
@ExcelExport("IP来源")
private String requestIp;

@TableField(value = "time")
@ApiModelProperty(value = "耗时")
@ExcelExport("耗时")
private Long time;

@TableField(value = "username")
@ApiModelProperty(value = "用户名")
@ExcelExport("用户名")
private String username;

@TableField(value = "address")
@ApiModelProperty(value = "IP归属地")
@ExcelExport("IP归属地")
private String address;

@TableField(value = "browser")
@ApiModelProperty(value = "浏览器")
@ExcelExport("浏览器")
private String browser;

@TableField(value = "exception_detail")
@ApiModelProperty(value = "异常信息")
@ExcelExport("异常信息")
private String exceptionDetail;

@TableField(value = "create_time")
@ApiModelProperty(value = "创建时间")
@ExcelExport("创建时间")
private Timestamp createTime;

private static final long serialVersionUID = 1L;

public static final String COL_LOG_ID = "log_id";

public static final String COL_DESCRIPTION = "description";

public static final String COL_LOG_TYPE = "log_type";

public static final String COL_METHOD = "method";

public static final String COL_PARAMS = "params";

public static final String COL_REQUEST_IP = "request_ip";

public static final String COL_TIME = "time";

public static final String COL_USERNAME = "username";

public static final String COL_ADDRESS = "address";

public static final String COL_BROWSER = "browser";

public static final String COL_EXCEPTION_DETAIL = "exception_detail";

public static final String COL_CREATE_TIME = "create_time";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fun.yizhierha.monitor.domain.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.sql.Timestamp;

@Data
@ApiModel("查询操作日志vo")
public class RetrieveSysLogVo{

@ApiModelProperty("描述")
private String description;

@ApiModelProperty("日志类型")
private String logType;

@ApiModelProperty("参数")
private String params;

@ApiModelProperty("IP来源")
private String requestIp;

@ApiModelProperty("用户名")
private String username;

@ApiModelProperty("IP归属地")
private String address;

@ApiModelProperty("浏览器")
private String browser;

@ApiModelProperty(value = "起始耗时")
private Long startTime;

@ApiModelProperty(value = "结束耗时")
private Long endTime;

@ApiModelProperty(value = "起始创建时间",dataType = "String",example = "2022-03-03")
private Timestamp startCreateTime;

@ApiModelProperty(value = "结束创建时间",dataType = "String",example = "2022-03-22")
private Timestamp endCreateTime;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fun.yizhierha.monitor.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import fun.yizhierha.monitor.domain.SysLog;
import org.apache.ibatis.annotations.Mapper;

/** generated by EH-Admin
* @author 二哈
* @date Fri Dec 09 20:47:12 CST 2022
**/
@Mapper
public interface SysLogMapper extends BaseMapper<SysLog>{
}
Loading

0 comments on commit 3c0b2e5

Please sign in to comment.