-----------------------------------------------------------------------------
做项目时写的日志功能,但是还是有很多BUG,勉强能用,有浏览的大神如有更好的文章还请留言推荐下
技术新人,编码潦草,不喜勿喷,谢谢看客大佬
---------------------------
package com.wcjy.statLogger;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import com.wcjy.base.entity.Admin;
import com.wcjy.core.annotation.PrimaryKey;
import com.wcjy.core.logbook.entity.Logbook;
import com.wcjy.core.logbook.service.LogbookService;
@Aspect
public class LogAspect {
/** 描述 (@author: WangJiShun) */
private static final long serialVersionUID = 1L;
private static final Logger logger = Logger.getLogger(LogAspect.class);
public Integer id=null;
@Autowired
private LogbookService logbookService;
/**
* 管理员登录方法的切入点
*/
//execution(* com.wcjy.*.*.service..*Impl.*(..))
@Pointcut("execution(* com.wcjy.*.*.service.*.login(..))")
public void loginCell(){
}
/**
* 添加业务逻辑方法切入点
*/
@Pointcut("execution(* com.wcjy.*.*.service.*.save(..))")
public void insertCell() {
}
/**
* 修改业务逻辑方法切入点
*/
@Pointcut("execution(* com.wcjy.*.*.service.*.update(..))")
public void updateCell() {
}
/**
* 删除业务逻辑方法切入点
*/
@Pointcut("execution(* com.wcjy.*.*.service.*.delete(..))")
public void deleteCell() {
}
/**
* 添加操作日志(后置通知)
*
* @param joinPoint
* @param object
*/
@AfterReturning(value = "insertCell()", argNames = "object", returning = "object")
public void insertLog(JoinPoint joinPoint, Object object) throws Throwable {
Admin admin=(Admin)ServletActionContext.getServletContext().getAttribute("admin");
String servletPath = ServletActionContext.getRequest().getServletPath();
servletPath = StringUtils.substringBeforeLast(servletPath, ".");// 去掉后面的后缀
// 判断参数
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 获取操作内容
String opContent = optionContent(joinPoint.getArgs(), methodName);
// 创建日志对象
Logbook logbook = new Logbook();
logbook.setUser(admin.getUserAccount());
logbook.setModule(methodName);
logbook.setIp(InetAddress.getLocalHost().getHostAddress());
logbook.setEditurl(servletPath);
logbook.setAction("新增");
logbook.setTitle(logbookService.getByUrl(servletPath));
logbook.setDataId(opContent);
logbookService.insert(logbook);
}
/**
* 修改操作日志(后置通知)
*
* @param joinPoint
* @param object
* @throws Throwable
*/
@AfterReturning(value = "updateCell()", argNames = "object", returning = "object")
public void updateLog(JoinPoint joinPoint, Object object) throws Throwable {
Admin admin=(Admin)ServletActionContext.getServletContext().getAttribute("admin");
String servletPath = ServletActionContext.getRequest().getServletPath();
servletPath = StringUtils.substringBeforeLast(servletPath, ".");// 去掉后面的后缀
// 判断参数
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 获取操作内容
String opContent = optionContent(joinPoint.getArgs(), methodName);
// 创建日志对象
Logbook logbook = new Logbook();
logbook.setUser(admin.getUserAccount());
logbook.setModule(methodName);
logbook.setIp(InetAddress.getLocalHost().getHostAddress());
logbook.setEditurl(servletPath);
logbook.setAction("修改/编辑");
logbook.setTitle(logbookService.getByUrl(servletPath));
logbook.setDataId(opContent);
logbookService.insert(logbook);
}
/**
* 删除操作
*
* @param joinPoint
* @param object
*/
@AfterReturning(value = "deleteCell()", argNames = "object", returning = "object")
public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable {
Admin admin=(Admin)ServletActionContext.getServletContext().getAttribute("admin");
String servletPath = ServletActionContext.getRequest().getServletPath();
servletPath = StringUtils.substringBeforeLast(servletPath, ".");// 去掉后面的后缀
// 判断参数
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
// 获取方法名
String methodName = joinPoint.getSignature().getName();
StringBuffer rs = new StringBuffer();
String className = null;
for (Object info : joinPoint.getArgs()) {
// 获取对象类型
className = info.getClass().getName();
className = className.substring(className.lastIndexOf(".") + 1);
rs.append("id:" + joinPoint.getArgs()[0] );
}
// 创建日志对象
Logbook logbook = new Logbook();
logbook.setUser(admin.getUserAccount());
logbook.setModule(methodName);
logbook.setIp(InetAddress.getLocalHost().getHostAddress());
logbook.setEditurl(servletPath);
logbook.setAction("删除");
logbook.setTitle(logbookService.getByUrl(servletPath));
logbook.setDataId(rs.toString());
logbookService.insert(logbook);
}
/**
* 使用Java反射来获取被拦截方法(insert、update)的参数值, 将参数值拼接为操作内容
*
* @param args
* @param mName
* @return
*/
public String optionContent(Object[] args, String mName) {
if (args == null) {
return null;
}
StringBuffer rs = new StringBuffer();
// 遍历参数对象
for (Object info : args) {
// 获取对象的所有方法
Method[] methods = info.getClass().getDeclaredMethods();
// 遍历方法,判断get方法
for (Method method : methods) {
String methodName = method.getName();
// 判断是不是getXXXXId方法
if (methodName.indexOf("get") == -1 || methodName.lastIndexOf("Id") != (methodName.length()-2)) {// 不是get方法
continue;// 不处理
}
PrimaryKey pk = method.getAnnotation(PrimaryKey.class);
if (pk == null) {
continue;// 不处理
}
Object rsValue = null;
try {
// 调用get方法,获取返回值
rsValue = method.invoke(info);
} catch (Exception e) {
continue;
}
// 将值加入内容中
rs.append( methodName + ":" + rsValue + " ");
}
}
return rs.toString();
}
}