pull/1121/head
liujiang 2025-11-24 23:36:59 +08:00
commit 65b80e3005
4 changed files with 120 additions and 59 deletions

View File

@ -116,7 +116,7 @@ public class LogAspect
// 保存数据库
// AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
// 输出到日志文件
log.info("【接口请求日志】{}", operLog);
log.info("【接口日志】{}", operLog);
}
catch (Exception exp)

View File

@ -7,9 +7,9 @@ import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.notice.AbstractNotice;
import com.ruoyi.framework.notice.fs.entity.*;
import com.ruoyi.system.domain.SysOperLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -58,39 +58,29 @@ public class FsNotice extends AbstractNotice {
/**
*
*
* @param e
* @param optLog
*/
public <T extends Exception> void sendException2MonitorChat(T e) {
sendException2MonitorChat(null, e);
}
/**
*
*
* @param e
*/
public <T extends Exception> void sendException2MonitorChat(String uri, T e) {
public void sendException2MonitorChat(final SysOperLog optLog) {
if (Boolean.TRUE.equals(monitorSwitch)) {
String username = SecurityUtils.getUsernameSafe();
ThreadUtil.execAsync(() -> {
try {
//内容
FeiShuMsg.ZhCn zhCn = new FeiShuMsg.ZhCn();
List<List<FeiShuMsg.BaseField>> contentFields = new ArrayList<>(6);
List<List<FeiShuMsg.BaseField>> contentFields = new ArrayList<>(7);
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("环境:{}", env))));
if (uri != null) {
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("地址:{}", uri))));
}
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("用户:{}", username))));
.createText(CharSequenceUtil.format("地址:{}", optLog.getOperUrl()))));
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("时间:{}", DateUtil.now()))));
.createText(CharSequenceUtil.format("入参:{}", StrUtil.truncateUtf8(optLog.getOperParam(), 512)))));
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("异常:{}", e.getClass().getName()))));
.createText(CharSequenceUtil.format("用户:{}", optLog.getOperName()))));
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("错误信息:{}", StrUtil.truncateUtf8(e.getMessage(), 512)))));
.createText(CharSequenceUtil.format("时间:{}", DateUtil.formatDateTime(optLog.getOperTime())))));
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("异常:{}", optLog.getTitle()))));
contentFields.add(Collections.singletonList(FeiShuTextField
.createText(CharSequenceUtil.format("错误信息:{}", StrUtil.truncateUtf8(optLog.getErrorMsg(), 512)))));
zhCn.setContent(contentFields);
//消息体
FeiShuMsg feiShuMsg = new FeiShuMsg();
@ -106,7 +96,6 @@ public class FsNotice extends AbstractNotice {
}
}
/**
*
*

View File

@ -1,14 +1,23 @@
package com.ruoyi.framework.web.exception;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.DemoModeException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.html.EscapeUtil;
import com.ruoyi.common.utils.http.HttpHelper;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.aspectj.LogAspect;
import com.ruoyi.framework.notice.fs.FsNotice;
import com.ruoyi.system.domain.SysOperLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
@ -21,6 +30,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
*
@ -38,9 +50,9 @@ public class GlobalExceptionHandler
@ExceptionHandler(AccessDeniedException.class)
public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',权限校验失败'{}'", optLog.getOperUrl(), e.getMessage());
sendExceptionMsg(optLog);
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
}
@ -51,9 +63,9 @@ public class GlobalExceptionHandler
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',不支持'{}'请求", optLog.getOperUrl(), e.getMethod());
sendExceptionMsg(optLog);
return AjaxResult.error(e.getMessage());
}
@ -63,9 +75,9 @@ public class GlobalExceptionHandler
@ExceptionHandler(ServiceException.class)
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生业务异常'{}'", requestURI, e.getMessage());
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',发生业务异常'{}'", optLog.getOperUrl(), e.getMessage());
sendExceptionMsg(optLog);
Integer code = e.getCode();
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
}
@ -76,9 +88,9 @@ public class GlobalExceptionHandler
*/
@ExceptionHandler(IllegalArgumentException.class)
public AjaxResult illegalArgumentException(IllegalArgumentException e, HttpServletRequest request) {
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生校验异常'{}'", requestURI, e.getMessage());
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',发生校验异常'{}'", optLog.getOperUrl(), e.getMessage());
sendExceptionMsg(optLog);
return AjaxResult.error(e.getMessage());
}
@ -88,9 +100,9 @@ public class GlobalExceptionHandler
@ExceptionHandler(MissingPathVariableException.class)
public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e);
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", optLog.getOperUrl(), e);
sendExceptionMsg(optLog);
return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName()));
}
@ -100,14 +112,14 @@ public class GlobalExceptionHandler
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
SysOperLog optLog = createOptLog(e, request);
String value = Convert.toStr(e.getValue());
if (StringUtils.isNotEmpty(value))
{
value = EscapeUtil.clean(value);
}
log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e);
sendExceptionMsg(requestURI, e);
log.error("请求参数类型不匹配'{}',发生系统异常.", optLog.getOperUrl(), e);
sendExceptionMsg(optLog);
return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getName(), e.getRequiredType().getName(), value));
}
@ -117,9 +129,9 @@ public class GlobalExceptionHandler
@ExceptionHandler(RuntimeException.class)
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生未知异常.", requestURI, e);
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',发生未知异常.", optLog.getOperUrl(), e);
sendExceptionMsg(optLog);
return AjaxResult.error(e.getMessage());
}
@ -129,9 +141,9 @@ public class GlobalExceptionHandler
@ExceptionHandler(Exception.class)
public AjaxResult handleException(Exception e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
log.error("请求地址'{}',发生系统异常.", requestURI, e);
sendExceptionMsg(requestURI, e);
SysOperLog optLog = createOptLog(e, request);
log.error("请求地址'{}',发生系统异常.", optLog.getOperUrl(), e);
sendExceptionMsg(optLog);
return AjaxResult.error(e.getMessage());
}
@ -141,9 +153,10 @@ public class GlobalExceptionHandler
@ExceptionHandler(BindException.class)
public AjaxResult handleBindException(BindException e, HttpServletRequest request)
{
SysOperLog optLog = createOptLog(e, request);
log.error(e.getMessage(), e);
String message = e.getAllErrors().get(0).getDefaultMessage();
sendExceptionMsg(request.getRequestURI(), e);
sendExceptionMsg(optLog);
return AjaxResult.error(message);
}
@ -153,10 +166,10 @@ public class GlobalExceptionHandler
@ExceptionHandler(MethodArgumentNotValidException.class)
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request)
{
String requestURI = request.getRequestURI();
SysOperLog optLog = createOptLog(e, request);
String message = e.getBindingResult().getFieldError().getDefaultMessage();
log.error("请求地址'{}',发生校验异常: {}", requestURI, message);
sendExceptionMsg(requestURI, e);
log.error("请求地址'{}',发生校验异常: {}", optLog.getOperUrl(), message);
sendExceptionMsg(optLog);
return AjaxResult.error(message);
}
@ -172,11 +185,69 @@ public class GlobalExceptionHandler
/**
*
*
* @param uri
* @param e
* @param <T>
* @param optLog
*/
private <T extends Exception> void sendExceptionMsg(String uri, T e) {
SpringUtils.getBean(FsNotice.class).sendException2MonitorChat(uri, e);
private void sendExceptionMsg(SysOperLog optLog) {
log.info("【异常日志】{}", optLog);
SpringUtils.getBean(FsNotice.class).sendException2MonitorChat(optLog);
}
private <T extends Exception> SysOperLog createOptLog(T e, HttpServletRequest request) {
SysOperLog syslog = new SysOperLog();
syslog.setTitle(e.getClass().getName());
syslog.setRequestMethod(request.getMethod());
syslog.setOperName(SecurityUtils.getUsernameSafe());
syslog.setOperUrl(request.getRequestURI());
syslog.setOperIp(IpUtils.getIpAddr(request));
syslog.setOperParam(JSONUtil.toJsonStr(getParamsMap(request)));
syslog.setStatus(1);
syslog.setErrorMsg(e.getMessage());
syslog.setOperTime(new Date());
return syslog;
}
/**
*
*
* @param request
* @return
*/
private Map<String, Object> getParamsMap(HttpServletRequest request) {
Map<String, Object> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
if (MapUtil.isNotEmpty(requestParams)) {
for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
String name = entry.getKey();
if (ArrayUtil.contains(LogAspect.EXCLUDE_PROPERTIES, name)) {
//敏感信息不记入日志
continue;
}
String[] values = entry.getValue();
StringBuilder valueStrBuilder = new StringBuilder();
if (values != null) {
for (int i = 0; i < values.length; i++) {
valueStrBuilder.append(values[i]);
if (i != values.length - 1) {
valueStrBuilder.append(",");
}
}
}
params.put(name, valueStrBuilder.toString());
}
}
String requestBodyStr = HttpHelper.getBodyString(request);
if (JSONUtil.isTypeJSONObject(requestBodyStr)) {
JSONObject requestBody = JSONUtil.parseObj(requestBodyStr);
for (Map.Entry<String, Object> entry : requestBody.entrySet()) {
String name = entry.getKey();
if (ArrayUtil.contains(LogAspect.EXCLUDE_PROPERTIES, name)) {
//敏感信息不记入日志
continue;
}
Object value = entry.getValue();
params.put(name, value);
}
}
return params;
}
}

View File

@ -1,18 +1,19 @@
package com.ruoyi.system.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Excel.ColumnType;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.ToString;
import java.util.Date;
/**
* oper_log
*
* @author ruoyi
*/
@ToString(callSuper = false)
@ToString(callSuper = false, exclude = {"operId"})
public class SysOperLog extends BaseEntity
{
private static final long serialVersionUID = 1L;