浙江报告与日志功能
This commit is contained in:
@@ -1,153 +0,0 @@
|
||||
package com.njcn.gather.system.log.aop;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.db.mybatisplus.constant.UserConstant;
|
||||
import com.njcn.gather.system.log.pojo.enums.LogLevelEnum;
|
||||
import com.njcn.gather.system.log.pojo.enums.LogOperationTypeEnum;
|
||||
import com.njcn.gather.system.log.pojo.po.SysLogAudit;
|
||||
import com.njcn.gather.system.log.service.ISysLogAuditService;
|
||||
import com.njcn.gather.system.pojo.enums.SystemResponseEnum;
|
||||
import com.njcn.gather.user.user.pojo.param.SysUserParam;
|
||||
import com.njcn.gather.user.user.pojo.po.SysUser;
|
||||
import com.njcn.gather.user.user.service.ISysUserService;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
* @data 2024-12-2
|
||||
*/
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class LogAdvice implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private final ISysLogAuditService sysLogAuditService;
|
||||
private final ISysUserService sysUserService;
|
||||
private final List<String> IGNORE_METHOD = new ArrayList<>(Arrays.asList("login"));
|
||||
|
||||
private BlockingQueue<SysLogAudit> logQueue = new LinkedBlockingDeque<>();
|
||||
|
||||
// @Pointcut(value = "execution(* com.njcn.gather..controller.*.*(..)) && !execution(* com.njcn.gather..controller.AuthController.*(..))")
|
||||
@Pointcut(value = "execution(* com.njcn.gather..controller.*.*(..))")
|
||||
public void logPointcut() {
|
||||
}
|
||||
|
||||
@Around("logPointcut()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
String name = joinPoint.getSignature().getName();
|
||||
String username = "";
|
||||
// 因为刚开始登录时无法获取到token,针对login方法单独处理
|
||||
if (IGNORE_METHOD.contains(name)) {
|
||||
Object[] args = joinPoint.getArgs();
|
||||
Object arg = args[0];
|
||||
if (arg instanceof SysUserParam.LoginParam) {
|
||||
username = ((SysUserParam.LoginParam) arg).getUsername();
|
||||
}
|
||||
} else {
|
||||
String userId = RequestUtil.getUserId();
|
||||
if (StrUtil.isNotBlank(userId)) {
|
||||
SysUser user = sysUserService.getById(RequestUtil.getUserId());
|
||||
if (ObjectUtil.isNotNull(user)) {
|
||||
username = user.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object result = null;
|
||||
try {
|
||||
sysLogAuditService.scheduleRemoveLog();
|
||||
result = joinPoint.proceed();
|
||||
if (result instanceof HttpResult) {
|
||||
HttpResult result1 = (HttpResult) result;
|
||||
if (CommonResponseEnum.SUCCESS.getCode().equals(result1.getCode())) {
|
||||
addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, LogOperationTypeEnum.OPERATE.getMsg(), 1, LogLevelEnum.INFO.getCode(), 0);
|
||||
} else {
|
||||
addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, LogOperationTypeEnum.OPERATE.getMsg(), 0, LogLevelEnum.INFO.getCode(), 0);
|
||||
}
|
||||
} else {
|
||||
addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, LogOperationTypeEnum.OPERATE.getMsg(), 1, LogLevelEnum.INFO.getCode(), 0);
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
addLogToQueue(joinPoint, "".equals(username) ? UserConstant.UNKNOWN_USER_ID : username, LogOperationTypeEnum.OPERATE.getMsg(), 0, LogLevelEnum.ERROR.getCode(), 1);
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void addLogToQueue(ProceedingJoinPoint joinPoint, String username, String operationType, Integer result, Integer level, Integer warn) {
|
||||
SysLogAudit sysLogAudit = new SysLogAudit();
|
||||
sysLogAudit.setOperateType(operationType);
|
||||
sysLogAudit.setLevel(level);
|
||||
sysLogAudit.setWarn(warn); //0-未告警,1-告警
|
||||
sysLogAudit.setCreateBy(username);
|
||||
|
||||
sysLogAudit.setIp(RequestUtil.getUserIp());
|
||||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
OperateInfo operateInfo = method.getAnnotation(OperateInfo.class);
|
||||
ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
|
||||
String resultStr = (result == 1 ? "成功" : "失败");
|
||||
if (operateInfo != null) {
|
||||
//注解上的操作类型
|
||||
sysLogAudit.setResult(operateInfo.operateType() + resultStr);
|
||||
}
|
||||
if (apiOperation != null) {
|
||||
//注解上的描述
|
||||
String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
sysLogAudit.setRemark(username + ":" + now + " " + apiOperation.value() + " " + resultStr);
|
||||
}
|
||||
//Object[] args = joinPoint.getArgs();
|
||||
logQueue.add(sysLogAudit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
List<SysLogAudit> logList = new ArrayList<>();
|
||||
SysLogAudit log;
|
||||
while ((log = logQueue.poll(5, TimeUnit.MILLISECONDS)) != null) {
|
||||
log.setSort(sysLogAuditService.getMaxSort() + 1);
|
||||
logList.add(log);
|
||||
}
|
||||
if (!logList.isEmpty()) {
|
||||
sysLogAuditService.saveBatch(logList); // 假设有一个批量保存的方法
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(SystemResponseEnum.LOG_RECORD_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,16 @@ public class SysLogAudit implements Serializable {
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 操作类型
|
||||
* 登录名
|
||||
*/
|
||||
private String operateType;
|
||||
private String loginName;
|
||||
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
|
||||
/**
|
||||
* IP
|
||||
@@ -38,14 +45,31 @@ public class SysLogAudit implements Serializable {
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 事件结果
|
||||
* 操作内容
|
||||
*/
|
||||
private String operate;
|
||||
|
||||
|
||||
/**
|
||||
* 操作类型 (比如:查询、新增、删除、下载等等)
|
||||
*/
|
||||
private String operateType;
|
||||
|
||||
/**
|
||||
* 事件结果 0失败 1成功
|
||||
*/
|
||||
private String result;
|
||||
|
||||
/**
|
||||
* 事件描述
|
||||
* 失败原因
|
||||
*/
|
||||
private String remark;
|
||||
private String reason;
|
||||
|
||||
/**
|
||||
* 事件类型
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
|
||||
/**
|
||||
* 事件严重度(0.普通 1.中等 2.严重)
|
||||
@@ -57,25 +81,14 @@ public class SysLogAudit implements Serializable {
|
||||
*/
|
||||
private Integer warn;
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
|
||||
private Integer sort;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
* 日志发生事件
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
|
||||
@JsonSerialize(using = LocalDateTimeSerializer.class)
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime logTime;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,15 @@ package com.njcn.gather.system.log.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.gather.system.log.pojo.param.SysLogParam;
|
||||
import com.njcn.gather.system.log.pojo.po.SysLogAudit;
|
||||
import com.njcn.gather.system.log.pojo.vo.SysLogVO;
|
||||
import io.swagger.models.auth.In;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
|
||||
/**
|
||||
* @author caozehui
|
||||
@@ -26,22 +33,20 @@ public interface ISysLogAuditService extends IService<SysLogAudit> {
|
||||
*/
|
||||
void exportCSV(SysLogParam.QueryParam param);
|
||||
|
||||
/**
|
||||
* 获取最大的排序值
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Integer getMaxSort();
|
||||
|
||||
/**
|
||||
* 按照 一定规则 清除日志
|
||||
*/
|
||||
void scheduleRemoveLog();
|
||||
|
||||
/**
|
||||
* 分析日志
|
||||
*
|
||||
* @param param
|
||||
*/
|
||||
void analyse(SysLogParam.QueryParam param);
|
||||
|
||||
void recodeAdviceLog(MethodParameter returnType, HttpResult<?> httpResult, String methodDescribe);
|
||||
|
||||
/**
|
||||
* 全局异常拦截器的捕获的异常进行日志记录入库
|
||||
*
|
||||
* @param businessException 异常
|
||||
* @param message 异常描述
|
||||
*/
|
||||
void recodeBusinessExceptionLog(Exception businessException, String message);
|
||||
}
|
||||
|
||||
@@ -6,31 +6,41 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.common.pojo.constant.LogInfo;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.PubUtils;
|
||||
import com.njcn.db.mybatisplus.constant.UserConstant;
|
||||
import com.njcn.gather.system.log.mapper.SysLogAuditMapper;
|
||||
import com.njcn.gather.system.log.pojo.enums.LogLevelEnum;
|
||||
import com.njcn.gather.system.log.pojo.param.SysLogParam;
|
||||
import com.njcn.gather.system.log.pojo.po.SysLogAudit;
|
||||
import com.njcn.gather.system.log.service.ISysLogAuditService;
|
||||
import com.njcn.gather.system.log.util.CSVUtil;
|
||||
import com.njcn.gather.user.user.pojo.po.SysUser;
|
||||
import com.njcn.gather.user.user.service.ISysUserService;
|
||||
import com.njcn.web.factory.PageFactory;
|
||||
import com.njcn.web.utils.HttpServletUtil;
|
||||
import com.njcn.web.utils.ReflectCommonUtil;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -42,6 +52,8 @@ import java.util.stream.Collectors;
|
||||
@RequiredArgsConstructor
|
||||
public class SysLogAuditServiceImpl extends ServiceImpl<SysLogAuditMapper, SysLogAudit> implements ISysLogAuditService {
|
||||
|
||||
private final ISysUserService sysUserService;
|
||||
|
||||
@Override
|
||||
public Page<SysLogAudit> listSysLogAudit(SysLogParam.QueryParam param) {
|
||||
QueryWrapper<SysLogAudit> queryWrapper = new QueryWrapper<>();
|
||||
@@ -57,55 +69,41 @@ public class SysLogAuditServiceImpl extends ServiceImpl<SysLogAuditMapper, SysLo
|
||||
|
||||
@Override
|
||||
public void exportCSV(SysLogParam.QueryParam param) {
|
||||
String[] titles = {"日志类型", "IP", "事件结果", "描述", "日志等级", "告警标准", "操作用户", "记录时间"};
|
||||
String[] keys = {"operateType", "ip", "result", "remark", "level", "warn", "createBy", "createTime"};
|
||||
QueryWrapper<SysLogAudit> queryWrapper = new QueryWrapper<>();
|
||||
if (ObjectUtil.isNotNull(param)) {
|
||||
queryWrapper
|
||||
.eq(StrUtil.isNotBlank(param.getOperateType()), "sys_log_audit.Operate_Type", param.getOperateType())
|
||||
.eq(StrUtil.isNotBlank(param.getCreateBy()), "sys_log_audit.Create_By", param.getCreateBy())
|
||||
.between(StrUtil.isAllNotBlank(param.getSearchBeginTime(), param.getSearchEndTime()), "sys_log_audit.Create_Time", param.getSearchBeginTime(), param.getSearchEndTime());
|
||||
}
|
||||
queryWrapper.orderByDesc("sys_log_audit.Create_Time");
|
||||
List<SysLogAudit> list = this.list(queryWrapper);
|
||||
List<Map<String, Object>> data = list.stream().map(item -> {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("operateType", item.getOperateType());
|
||||
map.put("ip", item.getIp());
|
||||
map.put("result", item.getResult());
|
||||
map.put("remark", item.getRemark());
|
||||
map.put("level", LogLevelEnum.getEnum(item.getLevel()).getMsg());
|
||||
map.put("warn", item.getWarn() == 0 ? "未告警" : "告警");
|
||||
map.put("createBy", item.getCreateBy());
|
||||
//将 createTime 转换为 yyyy-MM-dd HH:mm:ss 格式
|
||||
map.put("createTime", item.getCreateTime() == null ? "" : item.getCreateTime().toString().replace("T", " "));
|
||||
return map;
|
||||
}).collect(Collectors.toList());
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
CSVUtil.export("日志数据" + sdf.format(new Date()) + ".csv", titles, keys, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMaxSort() {
|
||||
SysLogAudit log = this.lambdaQuery().orderByDesc(SysLogAudit::getSort).last("limit 1").one();
|
||||
if (ObjectUtil.isNotNull(log)) {
|
||||
return log.getSort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scheduleRemoveLog() {
|
||||
QueryWrapper<SysLogAudit> wrapper = new QueryWrapper<>();
|
||||
wrapper.lt("Create_Time", LocalDateTime.now().minusDays(30 * 6));
|
||||
int count = this.count();
|
||||
if (count > 1e6) {
|
||||
wrapper.or().orderByAsc("Create_Time").last("limit " + (count - 1e6));
|
||||
}
|
||||
this.remove(wrapper);
|
||||
// String[] titles = {"日志类型", "IP", "事件结果", "描述", "日志等级", "告警标准", "操作用户", "记录时间"};
|
||||
// String[] keys = {"operateType", "ip", "result", "remark", "level", "warn", "createBy", "createTime"};
|
||||
// QueryWrapper<SysLogAudit> queryWrapper = new QueryWrapper<>();
|
||||
// if (ObjectUtil.isNotNull(param)) {
|
||||
// queryWrapper
|
||||
// .eq(StrUtil.isNotBlank(param.getOperateType()), "sys_log_audit.Operate_Type", param.getOperateType())
|
||||
// .eq(StrUtil.isNotBlank(param.getCreateBy()), "sys_log_audit.Create_By", param.getCreateBy())
|
||||
// .between(StrUtil.isAllNotBlank(param.getSearchBeginTime(), param.getSearchEndTime()), "sys_log_audit.Create_Time", param.getSearchBeginTime(), param.getSearchEndTime());
|
||||
// }
|
||||
// queryWrapper.orderByDesc("sys_log_audit.Create_Time");
|
||||
// List<SysLogAudit> list = this.list(queryWrapper);
|
||||
// List<Map<String, Object>> data = list.stream().map(item -> {
|
||||
// Map<String, Object> map = new HashMap<>();
|
||||
// map.put("operateType", item.getOperateType());
|
||||
// map.put("ip", item.getIp());
|
||||
// map.put("result", item.getResult());
|
||||
// map.put("remark", item.getRemark());
|
||||
// map.put("level", LogLevelEnum.getEnum(item.getLevel()).getMsg());
|
||||
// map.put("warn", item.getWarn() == 0 ? "未告警" : "告警");
|
||||
// map.put("createBy", item.getCreateBy());
|
||||
// //将 createTime 转换为 yyyy-MM-dd HH:mm:ss 格式
|
||||
// map.put("createTime", item.getCreateTime() == null ? "" : item.getCreateTime().toString().replace("T", " "));
|
||||
// return map;
|
||||
// }).collect(Collectors.toList());
|
||||
// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
// CSVUtil.export("日志数据" + sdf.format(new Date()) + ".csv", titles, keys, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂时废弃,有需要再重新写
|
||||
*
|
||||
* @param param .
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public void analyse(SysLogParam.QueryParam param) {
|
||||
QueryWrapper<SysLogAudit> queryWrapper = new QueryWrapper<>();
|
||||
if (ObjectUtil.isNotNull(param)) {
|
||||
@@ -114,11 +112,113 @@ public class SysLogAuditServiceImpl extends ServiceImpl<SysLogAuditMapper, SysLo
|
||||
.between(StrUtil.isAllNotBlank(param.getSearchBeginTime(), param.getSearchEndTime()), "sys_log_audit.Create_Time", param.getSearchBeginTime(), param.getSearchEndTime());
|
||||
}
|
||||
List<SysLogAudit> list = this.list(queryWrapper);
|
||||
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(SysLogAudit::getCreateBy, Collectors.counting()));
|
||||
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(SysLogAudit::getLoginName, Collectors.counting()));
|
||||
|
||||
this.exportAnalyseExcel("分析结果", collect);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录controller全局拦截的日志,并非全局异常捕获的
|
||||
*
|
||||
* @param returnType 返回类型
|
||||
* @param httpResult 返回结果
|
||||
* @param methodDescribe 方法描述
|
||||
*/
|
||||
@Override
|
||||
public void recodeAdviceLog(MethodParameter returnType, HttpResult<?> httpResult, String methodDescribe) {
|
||||
SysLogAudit sysLogAudit = new SysLogAudit();
|
||||
String userId = RequestUtil.getUserId();
|
||||
if (StrUtil.isBlank(userId)) {
|
||||
String loginName = RequestUtil.getLoginName();
|
||||
if (loginName.equalsIgnoreCase(LogInfo.UNKNOWN_USER)) {
|
||||
sysLogAudit.setUserName(UserConstant.UN_LOGIN);
|
||||
sysLogAudit.setLoginName(UserConstant.UN_LOGIN);
|
||||
} else {
|
||||
sysLogAudit.setUserName(loginName);
|
||||
sysLogAudit.setLoginName(loginName);
|
||||
}
|
||||
} else {
|
||||
SysUser sysUser = sysUserService.getById(userId);
|
||||
if (sysUser != null) {
|
||||
sysLogAudit.setUserName(sysUser.getName());
|
||||
sysLogAudit.setLoginName(sysUser.getLoginName());
|
||||
} else {
|
||||
sysLogAudit.setUserName(UserConstant.UN_LOGIN);
|
||||
sysLogAudit.setLoginName(UserConstant.UN_LOGIN);
|
||||
}
|
||||
}
|
||||
// 不是很重要,本地部署的,除非以后工具类C端有必要,到时候再丰富读IP的过程
|
||||
sysLogAudit.setIp(RequestUtil.getUserIp());
|
||||
// 操作内容
|
||||
sysLogAudit.setOperate(methodDescribe);
|
||||
// 操作结果
|
||||
String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage();
|
||||
sysLogAudit.setResult(result);
|
||||
// 事件类型
|
||||
String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType();
|
||||
sysLogAudit.setType(type.equalsIgnoreCase("业务事件") ? 0 : 1);
|
||||
// 等级
|
||||
String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel();
|
||||
sysLogAudit.setLevel(levelStringToNumber(level));
|
||||
// 中等以上的需要置为未告警,但是告警功能暂未做
|
||||
if(sysLogAudit.getLevel() == 0){
|
||||
sysLogAudit.setWarn(1);
|
||||
}else{
|
||||
sysLogAudit.setWarn(0);
|
||||
}
|
||||
// 操作类型
|
||||
String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod());
|
||||
sysLogAudit.setOperateType(operateType);
|
||||
sysLogAudit.setLogTime(LocalDateTime.now());
|
||||
this.save(sysLogAudit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recodeBusinessExceptionLog(Exception exception, String message) {
|
||||
SysLogAudit sysLogAudit = new SysLogAudit();
|
||||
String userId = RequestUtil.getUserId();
|
||||
if (StrUtil.isBlank(userId)) {
|
||||
String loginName = RequestUtil.getLoginName();
|
||||
if (loginName.equalsIgnoreCase(LogInfo.UNKNOWN_USER)) {
|
||||
sysLogAudit.setUserName(UserConstant.UN_LOGIN);
|
||||
sysLogAudit.setLoginName(UserConstant.UN_LOGIN);
|
||||
} else {
|
||||
sysLogAudit.setUserName(loginName);
|
||||
sysLogAudit.setLoginName(loginName);
|
||||
}
|
||||
} else {
|
||||
SysUser sysUser = sysUserService.getById(userId);
|
||||
if (sysUser != null) {
|
||||
sysLogAudit.setUserName(sysUser.getName());
|
||||
sysLogAudit.setLoginName(sysUser.getLoginName());
|
||||
} else {
|
||||
sysLogAudit.setUserName(UserConstant.UN_LOGIN);
|
||||
sysLogAudit.setLoginName(UserConstant.UN_LOGIN);
|
||||
}
|
||||
}
|
||||
//根据异常获取method方法
|
||||
Method method = ReflectCommonUtil.getMethod(exception);
|
||||
if (exception instanceof MethodArgumentNotValidException) {
|
||||
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception;
|
||||
method = methodArgumentNotValidException.getParameter().getMethod();
|
||||
}
|
||||
// 不是很重要,本地部署的,除非以后工具类C端有必要,到时候再丰富读IP的过程
|
||||
sysLogAudit.setIp(RequestUtil.getUserIp());
|
||||
sysLogAudit.setReason(message);
|
||||
String result = CommonResponseEnum.FAIL.getMessage();
|
||||
sysLogAudit.setResult(result);
|
||||
String methodDescribe = ReflectCommonUtil.getMethodDescribeByMethod(method);
|
||||
sysLogAudit.setOperate(methodDescribe);
|
||||
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
|
||||
sysLogAudit.setType(type.equalsIgnoreCase("业务事件") ? 0 : 1);
|
||||
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
|
||||
sysLogAudit.setLevel(levelStringToNumber(level));
|
||||
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
|
||||
sysLogAudit.setOperateType(operateType);
|
||||
sysLogAudit.setLogTime(LocalDateTime.now());
|
||||
this.save(sysLogAudit);
|
||||
}
|
||||
|
||||
private void exportAnalyseExcel(String fileName, Map<String, Long> collect) {
|
||||
HttpServletResponse response = HttpServletUtil.getResponse();
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
@@ -140,95 +240,19 @@ public class SysLogAuditServiceImpl extends ServiceImpl<SysLogAuditMapper, SysLo
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 创建饼图
|
||||
// *
|
||||
// * @param sheet
|
||||
// * @param categoryList
|
||||
// * @param sheetDataList
|
||||
// */
|
||||
// private void createPieChart(XSSFSheet sheet, List<String> categoryList, List<Long> sheetDataList) {
|
||||
// int row1 = 0;
|
||||
// int row2 = 8;
|
||||
// int col1 = 0;
|
||||
// int col2 = 30;
|
||||
// // 设置图表列宽
|
||||
// for (int i = 0; i < row2; i++) {
|
||||
// sheet.setColumnWidth(i, 6000);
|
||||
// }
|
||||
// //y轴显示数据
|
||||
// String[] headArray = categoryList.stream().collect(Collectors.toList()).toArray(new String[]{});
|
||||
//
|
||||
// // Create a chart
|
||||
// XSSFDrawing drawing = sheet.createDrawingPatriarch();
|
||||
// ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, col1, row1, row2, col2);
|
||||
// XSSFChart chart = drawing.createChart(anchor);
|
||||
// //标题是否覆盖图表
|
||||
// chart.setTitleOverlay(false);
|
||||
// //设置图表标题
|
||||
// chart.setTitleText("分析结果");
|
||||
// // 创建图表系列
|
||||
// XDDFChartLegend legend = chart.getOrAddLegend();
|
||||
// legend.setPosition(LegendPosition.TOP);
|
||||
//
|
||||
// //动态数据
|
||||
// //x轴数据
|
||||
// XDDFDataSource<String> xData = XDDFDataSourcesFactory.fromArray(headArray);
|
||||
// //饼图数据
|
||||
// XDDFPieChartData data = (XDDFPieChartData) chart.createData(ChartTypes.PIE, null, null);
|
||||
// data.setVaryColors(true);
|
||||
//
|
||||
// Double[] yArray = sheetDataList.stream().collect(Collectors.toList()).toArray(new Double[]{});
|
||||
// //y轴数据
|
||||
// XDDFNumericalDataSource<Double> yData = XDDFDataSourcesFactory.fromArray(yArray);
|
||||
// XDDFChartData.Series series = data.addSeries(xData, yData);
|
||||
//
|
||||
// //series.setTitle("title", null);
|
||||
// series.setShowLeaderLines(true);
|
||||
// // 隐藏图例标识、系列名称、分类名称和数值
|
||||
// XDDFPieChartData.Series s = (XDDFPieChartData.Series) series;
|
||||
// CTPieSer ctPieSer = s.getCTPieSer();
|
||||
// showCateName(ctPieSer, false);
|
||||
// showVal(ctPieSer, false);
|
||||
// showLegendKey(ctPieSer, false);
|
||||
// showSerName(ctPieSer, false);
|
||||
//
|
||||
// chart.plot(data);
|
||||
// }
|
||||
|
||||
// 控制值系列名称是否显示
|
||||
private void showSerName(CTPieSer series, boolean val) {
|
||||
if (series.getDLbls().isSetShowSerName()) {
|
||||
series.getDLbls().getShowSerName().setVal(val);
|
||||
} else {
|
||||
series.getDLbls().addNewShowSerName().setVal(val);
|
||||
}
|
||||
}
|
||||
|
||||
// 控制分类名称是否显示
|
||||
private void showCateName(CTPieSer series, boolean val) {
|
||||
if (series.getDLbls().isSetShowCatName()) {
|
||||
series.getDLbls().getShowCatName().setVal(val);
|
||||
} else {
|
||||
series.getDLbls().addNewShowCatName().setVal(val);
|
||||
}
|
||||
}
|
||||
|
||||
// 控制值是否显示
|
||||
private void showVal(CTPieSer series, boolean val) {
|
||||
if (series.getDLbls().isSetShowVal()) {
|
||||
series.getDLbls().getShowVal().setVal(val);
|
||||
} else {
|
||||
series.getDLbls().addNewShowVal().setVal(val);
|
||||
}
|
||||
}
|
||||
|
||||
// 控制图例标识是否显示
|
||||
private void showLegendKey(CTPieSer series, boolean val) {
|
||||
if (series.getDLbls().isSetShowLegendKey()) {
|
||||
series.getDLbls().getShowLegendKey().setVal(val);
|
||||
} else {
|
||||
series.getDLbls().addNewShowLegendKey().setVal(val);
|
||||
/**
|
||||
* 严重度 文字 转 数字
|
||||
*/
|
||||
private Integer levelStringToNumber(String level) {
|
||||
switch (level) {
|
||||
case "中等":
|
||||
return 1;
|
||||
case "严重":
|
||||
return 2;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user