import java.lang.reflect.Method;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.mes.annotation.LogAnnotation;
import com.mes.dao.SysSystemLogMapper;
import com.mes.pojo.UserObject;
import com.mes.pojo.po.SysSystemLog;
@Component
@Aspect
public class LogAspect {
private Logger logger = LoggerFactory.getLogger(LogAspect.class);
@Resource
private SysSystemLogMapper mapper;
@Pointcut("execution(* com.mes.service..*(..))")
private void pointcut() {
}
@After(value = "pointcut()")
public void After(JoinPoint joinPoint) throws ClassNotFoundException {
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
Class<?> targetClass = Class.forName(className);
Method[] methods = targetClass.getMethods();
for (Method method : methods) {
if (method.getName().equalsIgnoreCase(methodName)) {
Class<?>[] clazzs = method.getParameterTypes();
if (clazzs.length == args.length) {
LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
if (logAnnotation != null) {
String logContent = logAnnotation.logContent();
String logModule = logAnnotation.logModule();
String logFunction = logAnnotation.logFunction();
int logType = logAnnotation.logType().ordinal();
logger.info("获取日志:{}-{}-{}", logModule, logFunction, logContent);
// 数据库记录操作...
UserObject userObject = (UserObject) SecurityUtils.getSubject().getSession().getAttribute("userObject");
// 未登录 不记录
if(userObject != null) {
SysSystemLog log = new SysSystemLog();
log.setIp(userObject.getUserRemoteIP());
log.setContent(logContent);
log.setModule(logModule);
log.setFunction(logFunction);
log.setType(logType+"");
log.setCreateId(Long.parseLong(userObject.getEmpId()));
log.setModifyId(Long.parseLong(userObject.getEmpId()));
Date date = new Date();
log.setCreateDate(date);
log.setModifyDate(date);
logger.info(log.toString());
mapper.insertSelective(log);
}
break;
}
}
}
}
}
}