日志预览

自定义注解
import java.lang.annotation.*;
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {
LogOperateTypeEnum operationType() ;
String operateContent();
}
自定义枚举(操作类型)
package com.cetron.common;
public enum LogOperateTypeEnum {
ADD("添加",1),
DEL("删除",2),
UPDATE("修改",3),
LOGIN("登陆",4);
private String operateDesc;
private int operateCode;
LogOperateTypeEnum(String operateDesc,int operateCode){
this.operateDesc=operateDesc;
this.operateCode=operateCode;
}
public static String getMessage(int operateCode){
for(LogOperateTypeEnum logOperateTypeEnum : LogOperateTypeEnum.values()){
if(logOperateTypeEnum.getOperateCode() == operateCode){
return logOperateTypeEnum.operateDesc;
}
}
return null;
}
public String getOperateDesc() {
return operateDesc;
}
public int getOperateCode() {
return operateCode;
}
public void setOperateDesc(String operateDesc) {
this.operateDesc = operateDesc;
}
public void setOperateCode(int operateCode) {
this.operateCode = operateCode;
}
}
获得ip地址的工具类
package com.cetron.common.utils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpUtil {
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress="";
}
return ipAddress;
}
}
在实现类的具体方法上加上注解
@Override
@Transactional
@LogAnnotation(operationType= LogOperateTypeEnum.ADD,operateContent="添加产品线")
public String addProductLine(String lineName) {
projectMapper.addProductLine(lineName);
return lineName;
}
切面
package com.cetron.aop;
improt ...
@Aspect
@Component
public class LogAspect {
@Resource
private LogMapper logMapper;
private Logger log = Logger.getLogger(getClass());
@Pointcut("@annotation(com.cetron.aop.LogAnnotation)")
public void operationLog(){}
@AfterReturning(value = "operationLog()",returning="returnValue")
public void doAfter(JoinPoint joinPoint,Object returnValue) {
LogVo logVo= getLogVo(joinPoint,returnValue);
}
private LogVo getLogVo(JoinPoint joinPoint,Object returnValue){
Subject subject = SecurityUtils.getSubject();
SysUserBaseVo user=null;
Long currentUserId=null;
if(subject.getPrincipal()!=null) {
user = (SysUserBaseVo) subject.getPrincipal();
currentUserId = user.getUserId();
}
else {
return null;
}
LogVo logVo = new LogVo();
String targetName = joinPoint.getTarget().getClass().getName();
Class targetClass = null;
LogAnnotation logAnnotation = null;
try {
targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
logAnnotation = method.getAnnotation(LogAnnotation.class);
break;
}
}
}
logVo.setLogType(logAnnotation.operationType().getOperateDesc());
if(returnValue.getClass()== HashMap.class){
logVo.setLogDesc(logAnnotation.operateContent());
}
else {
logVo.setLogDesc(logAnnotation.operateContent() + ":" + returnValue);
}
logVo.setLogStartDate(new Date());
logVo.setUserId(currentUserId);
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
logVo.setRequestIp(IpUtil.getIpAddr(request));
logMapper.addSysLog(logVo);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return logVo;
}
}
测试

结果
