Commit 2e0d026a by fsn

日志记录完善aop

parent d32a3558
......@@ -12,5 +12,14 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface BussinessLog {
/**
* 业务的名称,例如:"修改菜单"
*/
String value() default "";
/**
* 被修改的实体的唯一标识,例如:菜单实体的唯一标识为"id"
*/
String key() default "id";
}
package com.stylefeng.guns.common.annotion.log;
import java.lang.annotation.*;
/**
* 标记保存编辑操作之前的controller方法
*
* @author fengshuonan
* @date 2017-03-31 9:37
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface LogBefore {
}
......@@ -2,14 +2,18 @@ package com.stylefeng.guns.core.aop;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.core.support.StrKit;
import com.stylefeng.guns.core.util.Contrast;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.ToolUtil;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
......@@ -17,6 +21,7 @@ import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Map;
/**
* 日志记录
......@@ -38,9 +43,15 @@ public class LogAop {
public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
//获取拦截的方法名
MethodSignature ms = (MethodSignature) point.getSignature();
Method method = ms.getMethod();
String methodName = method.getName();
Signature sig = point.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = point.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
String methodName = currentMethod.getName();
//如果当前用户未登录,不做日志
ShiroUser user = ShiroKit.getUser();
......@@ -53,8 +64,9 @@ public class LogAop {
Object[] params = point.getArgs();
//获取操作名称
BussinessLog annotation = method.getAnnotation(BussinessLog.class);
BussinessLog annotation = currentMethod.getAnnotation(BussinessLog.class);
String bussinessName = annotation.value();
String key = annotation.key();
StringBuilder sb = new StringBuilder();
for (Object param : params) {
......@@ -62,10 +74,18 @@ public class LogAop {
sb.append(" & ");
}
String msg = ToolUtil.format("[时间]:{} [类名]:{} [方法]:{} [参数]:{}", DateUtil.getTime(), className, methodName, sb.toString());
msg = StrKit.removeSuffix(msg, "& ");
log.info(msg);
LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(),bussinessName,className,methodName,msg));
//如果涉及到修改,比对变化
String msg = null;
if (bussinessName.indexOf("修改") != -1) {
Object obj1 = LogObjectHolder.me().get();
Map<String, String> obj2 = HttpKit.getRequestParameters();
msg = Contrast.contrastObj(key, obj1, obj2);
} else {
msg = ToolUtil.format("[时间]:{} [类名]:{} [方法]:{} [参数]:{}", DateUtil.getTime(), className, methodName, sb.toString());
msg = StrKit.removeSuffix(msg, "& ");
}
LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(), bussinessName, className, methodName, msg));
return point.proceed();
}
}
\ No newline at end of file
package com.stylefeng.guns.core.util;
import com.stylefeng.guns.core.support.StrKit;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
/**
* 对比两个对象的变化的工具类
......@@ -45,4 +48,36 @@ public class Contrast {
}
return str;
}
public static String contrastObj(String key, Object pojo1, Map<String, String> pojo2) {
String value = pojo2.get(key);
String str = key + "=" + value + separator;
try {
Class clazz = pojo1.getClass();
Field[] fields = pojo1.getClass().getDeclaredFields();
int i = 1;
for (Field field : fields) {
if ("serialVersionUID".equals(field.getName())) {
continue;
}
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(pojo1);
Object o2 = pojo2.get(StrKit.firstCharToLowerCase(getMethod.getName().substring(3)));
if (o1 == null || o2 == null) {
continue;
}
if (!o1.toString().equals(o2.toString())) {
if (i != 1) {
str += separator;
}
str += "字段名称" + field.getName() + ",旧值:" + o1 + ",新值:" + o2;
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
\ No newline at end of file
package com.stylefeng.guns.modular.system.controller;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.state.MenuStatus;
import com.stylefeng.guns.common.constant.tips.Tip;
......@@ -9,7 +10,6 @@ import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.Contrast;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.MenuDao;
import com.stylefeng.guns.modular.system.warpper.MenuWarpper;
......@@ -78,6 +78,21 @@ public class MenuController extends BaseController {
}
/**
* 修该菜单
*/
@Permission(Const.ADMIN_NAME)
@RequestMapping(value = "/edit")
@ResponseBody
@BussinessLog("修改菜单")
public Tip edit(@Valid Menu menu, BindingResult result) {
if (result.hasErrors()) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
}
this.menuMapper.updateById(menu);
return SUCCESS_TIP;
}
/**
* 获取菜单列表
*/
@RequestMapping(value = "/list")
......@@ -116,25 +131,6 @@ public class MenuController extends BaseController {
}
/**
* 修该菜单
*/
@Permission(Const.ADMIN_NAME)
@RequestMapping(value = "/edit")
@ResponseBody
public Tip edit(@Valid Menu menu, BindingResult result) {
if (result.hasErrors()) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
}
Object obj1 = LogObjectHolder.me().get();
Object obj2 = menu;
System.out.println(Contrast.contrastObj(obj1,obj2));
this.menuMapper.updateById(menu);
return SUCCESS_TIP;
}
/**
* 删除菜单
*/
@Permission(Const.ADMIN_NAME)
......
......@@ -21,11 +21,11 @@
<plugins>
<!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->
<plugin interceptor="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
<property name="maxTime" value="100"/>
<!--SQL是否格式化 默认false -->
<property name="format" value="true"/>
</plugin>
<!--<plugin interceptor="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">-->
<!--<property name="maxTime" value="100"/>-->
<!--&lt;!&ndash;SQL是否格式化 默认false &ndash;&gt;-->
<!--<property name="format" value="true"/>-->
<!--</plugin>-->
<!-- SQL 执行分析拦截器 stopProceed 发现全表执行 delete update 是否停止运行 -->
<plugin interceptor="com.baomidou.mybatisplus.plugins.SqlExplainInterceptor">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment