格式化

master
RuoYi 7 years ago committed by Limy
parent a0a8c73fb8
commit 3128190f3d

@ -36,128 +36,142 @@ import com.ruoyi.project.system.user.domain.User;
@Aspect @Aspect
@Component @Component
@EnableAsync @EnableAsync
public class LogAspect { public class LogAspect
private static final Logger log = LoggerFactory.getLogger(LogAspect.class); {
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
@Autowired
private IOperLogService operLogService; @Autowired
private IOperLogService operLogService;
// 配置织入点
@Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.Log)") // 配置织入点
public void logPointCut() { @Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.Log)")
} public void logPointCut()
{
/** }
*
* /**
* @param joinPoint *
* *
*/ * @param joinPoint
@AfterReturning(pointcut = "logPointCut()") */
public void doBefore(JoinPoint joinPoint) { @AfterReturning(pointcut = "logPointCut()")
handleLog(joinPoint, null); public void doBefore(JoinPoint joinPoint)
} {
handleLog(joinPoint, null);
/** }
*
* /**
* @param joinPoint *
* @param e *
*/ * @param joinPoint
@AfterThrowing(value = "logPointCut()", throwing = "e") * @param e
public void doAfter(JoinPoint joinPoint, Exception e) { */
handleLog(joinPoint, e); @AfterThrowing(value = "logPointCut()", throwing = "e")
} public void doAfter(JoinPoint joinPoint, Exception e)
{
@Async handleLog(joinPoint, e);
private void handleLog(final JoinPoint joinPoint, final Exception e) { }
try {
// 获得注解 @Async
Log controllerLog = getAnnotationLog(joinPoint); private void handleLog(final JoinPoint joinPoint, final Exception e)
if (controllerLog == null) { {
return; try
} {
// 获得注解
// 获取当前的用户 Log controllerLog = getAnnotationLog(joinPoint);
User currentUser = ShiroUtils.getUser(); if (controllerLog == null)
{
// *========数据库日志=========*// return;
OperLog operLog = new OperLog(); }
operLog.setStatus(UserConstants.NORMAL);
// 请求的地址 // 获取当前的用户
String ip = ShiroUtils.getIp(); User currentUser = ShiroUtils.getUser();
operLog.setOperIp(ip);
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI()); // *========数据库日志=========*//
if (currentUser != null) { OperLog operLog = new OperLog();
operLog.setLoginName(currentUser.getLoginName()); operLog.setStatus(UserConstants.NORMAL);
operLog.setDeptName(currentUser.getDept().getDeptName()); // 请求的地址
} String ip = ShiroUtils.getIp();
operLog.setOperIp(ip);
if (e != null) { operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
operLog.setStatus(UserConstants.EXCEPTION); if (currentUser != null)
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); {
} operLog.setLoginName(currentUser.getLoginName());
// 设置方法名称 operLog.setDeptName(currentUser.getDept().getDeptName());
String className = joinPoint.getTarget().getClass().getName(); }
String methodName = joinPoint.getSignature().getName();
operLog.setMethod(className + "." + methodName + "()"); if (e != null)
// 处理设置注解上的参数 {
getControllerMethodDescription(controllerLog, operLog); operLog.setStatus(UserConstants.EXCEPTION);
// 保存数据库 operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
operLogService.insertOperlog(operLog); }
} catch (Exception exp) { // 设置方法名称
// 记录本地异常日志 String className = joinPoint.getTarget().getClass().getName();
log.error("==前置通知异常=="); String methodName = joinPoint.getSignature().getName();
log.error("异常信息:{}", exp.getMessage()); operLog.setMethod(className + "." + methodName + "()");
exp.printStackTrace(); // 处理设置注解上的参数
} getControllerMethodDescription(controllerLog, operLog);
} // 保存数据库
operLogService.insertOperlog(operLog);
/** }
* Controller catch (Exception exp)
* {
* @param joinPoint // 记录本地异常日志
* log.error("==前置通知异常==");
* @return log.error("异常信息:{}", exp.getMessage());
* @throws Exception exp.printStackTrace();
*/ }
public void getControllerMethodDescription(Log log, OperLog operLog) throws Exception { }
// 设置action动作
operLog.setAction(log.action()); /**
// 设置标题 * Controller
operLog.setTitle(log.title()); *
// 设置channel * @param joinPoint
operLog.setChannel(log.channel()); * @return
// 是否需要保存request参数和值 * @throws Exception
if (log.isSaveRequestData()) { */
// 获取参数的信息,传入到数据库中。 public void getControllerMethodDescription(Log log, OperLog operLog) throws Exception
setRequestValue(operLog); {
} // 设置action动作
} operLog.setAction(log.action());
// 设置标题
/** operLog.setTitle(log.title());
* log // 设置channel
* operLog.setChannel(log.channel());
* @param operLog // 是否需要保存request参数和值
* @param request if (log.isSaveRequestData())
*/ {
private void setRequestValue(OperLog operLog) { // 获取参数的信息,传入到数据库中。
Map<String, String[]> map = ServletUtils.getRequest().getParameterMap(); setRequestValue(operLog);
String params = JSONObject.toJSONString(map); }
operLog.setOperParam(StringUtils.substring(params, 0, 255)); }
}
/**
/** * log
* *
*/ * @param operLog
private Log getAnnotationLog(JoinPoint joinPoint) throws Exception { * @param request
Signature signature = joinPoint.getSignature(); */
MethodSignature methodSignature = (MethodSignature) signature; private void setRequestValue(OperLog operLog)
Method method = methodSignature.getMethod(); {
Map<String, String[]> map = ServletUtils.getRequest().getParameterMap();
if (method != null) { String params = JSONObject.toJSONString(map);
return method.getAnnotation(Log.class); operLog.setOperParam(StringUtils.substring(params, 0, 255));
} }
return null;
} /**
*
*/
private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
return method.getAnnotation(Log.class);
}
return null;
}
} }

Loading…
Cancel
Save