1.集成需要的依赖或jar
aspectjweaver-1.9.5.jar
spring-aop-4.3.16.RELEASE.jar
spring-aspects-4.2.0.RELEASE.jar
2.日志实体类
package com.macro.mall.bo;
/**
* Controller层的日志封装类
* Created by macro on 2018/4/26.
*/
public class WebLog {
/**
* 操作描述
*/
private String description;
/**
* 操作用户
*/
private String username;
/**
* 操作时间
*/
private Long startTime;
/**
* 消耗时间
*/
private Integer spendTime;
/**
* 根路径
*/
private String basePath;
/**
* URI
*/
private String uri;
/**
* URL
*/
private String url;
/**
* 请求类型
*/
private String method;
/**
* IP地址
*/
private String ip;
private Object parameter;
private Object result;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getStartTime() {
return startTime;
}
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
public Integer getSpendTime() {
return spendTime;
}
public void setSpendTime(Integer spendTime) {
this.spendTime = spendTime;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Object getParameter() {
return parameter;
}
public void setParameter(Object parameter) {
this.parameter = parameter;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
3.在applicationContext.xml中加入,基于注解
<aop:aspectj-autoproxy/>
4.WebLogAspect.java
package com.macro.mall.component;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONUtil;
import com.macro.mall.bo.WebLog;
import io.swagger.annotations.ApiOperation;
import net.logstash.logback.marker.Markers;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 统一日志处理切面
* Created by macro on 2018/4/26.
* @component 作用:类被spring管理
* @aspect 相当于aop:aspect/表示通知类在当前方法中
* Aspect:作用是把当前类标识为一个切面供容器读取
*
* 前置通知 @Before:该注解标注的方法在业务模块代码执行之前执行,其不能阻止业务模块的执行,除非抛出异常;
* 返回通知 @AfterReturning:该注解标注的方法在业务模块代码执行之后执行;
* 异常通知 @AfterThrowing:该注解标注的方法在业务模块抛出指定异常后执行;
* 后置通知 @After:该注解标注的方法在所有的Advice执行完成后执行,无论业务模块是否抛出异常,类似于finally的作用;
*
* 环绕通知 @Around:该注解功能最为强大,其所标注的方法用于编写包裹业务模块执行的代码,其可以传入一个
* ProceedingJoinPoint用于调用业务模块的代码,无论是调用前逻辑还是调用后逻辑,都可以在该方法中编写,
* 甚至其可以根据一定的条件而阻断业务模块的调用;
*
* // BusinessLog.@BusinessLog是否在方法上。如果在则返回true;不在则返回false。
* BusinessLog aspectLog = method.getAnnotation(BusinessLog.class);
* boolean flag = aspectLog.isStart();
*
* 此处采用isAnnotationPresent()方法
* 例子:A.isAnnotationPresent(B.class); B类型的注解是否在A类上。
*/
@Aspect
@Component
@Order(1)
public class WebLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);
/**
* 定义切点常量
* 此处的切点是注解的方式,也可以用包名的方式达到相同的效果
* @Pointcut("execution(* com.boss .springboot.service.impl.*.*(..))")
* 切点表达式中..两个点表明多个,*代表一个
* 上诉表达式代表切入com.boss.springboot.service.impl包下所有类所有方法,方法参数不限,返回类型不限。
* 第一个*代表返回类型不限,第二个*表示所有类,第三个*表示所有方法,..两个点表示方法里的参数不限。
* @Pointcut("execution(public * com.macro.mall.controller.*.*(..))")
public void webLog() {}
*/
/*拦截自定义注解类实现,这里拦截的是BusinessLog注解*/
private final String POINT_CUT = "@annotation(com.efiles.common.annotation.BusinessLog)";
@Pointcut(POINT_CUT)
public void webLog(){
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
}
@AfterReturning(value = "webLog()", returning = "ret")
public void doAfterReturning(Object ret) throws Throwable {
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
//获取当前请求对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//获取当前请求对象
HttpServletRequest request = attributes.getRequest();
//记录请求信息(通过Logstash传入Elasticsearch)
WebLog webLog = new WebLog();
//执行方法,获取返回参数
Object result = joinPoint.proceed();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
//获取方法 如:controller的login
Method method = methodSignature.getMethod();
if (method.isAnnotationPresent(BusinessLog.class)) {
//获取BusinessLog的注解
BusinessLog aspectLog = method.getAnnotation(BusinessLog.class);
webLog.setDescription(log.value());
}
long endTime = System.currentTimeMillis();
String urlStr = request.getRequestURL().toString();
webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
webLog.setIp(request.getRemoteUser());
webLog.setMethod(request.getMethod());
webLog.setParameter(getParameter(method, joinPoint.getArgs()));
webLog.setResult(result);
webLog.setSpendTime((int) (endTime - startTime));
webLog.setStartTime(startTime);
webLog.setUri(request.getRequestURI());
webLog.setUrl(request.getRequestURL().toString());
Map<String,Object> logMap = new HashMap<>();
logMap.put("url",webLog.getUrl());
logMap.put("method",webLog.getMethod());
logMap.put("parameter",webLog.getParameter());
logMap.put("spendTime",webLog.getSpendTime());
logMap.put("description",webLog.getDescription());
//在这里可以add到数据库,可以不同返回值
LOGGER.info(Markers.appendEntries(logMap), JSONUtil.parse(webLog).toString());
return result;
}
/**
* 根据方法和传入的参数获取请求参数
*/
private Object getParameter(Method method, Object[] args) {
List<Object> argList = new ArrayList<>();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
//将RequestBody注解修饰的参数作为请求参数
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
if (requestBody != null) {
argList.add(args[i]);
}
//将RequestParam注解修饰的参数作为请求参数
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestParam != null) {
Map<String, Object> map = new HashMap<>();
String key = parameters[i].getName();
if (!StringUtils.isEmpty(requestParam.value())) {
key = requestParam.value();
}
map.put(key, args[i]);
argList.add(map);
}
}
if (argList.size() == 0) {
return null;
} else if (argList.size() == 1) {
return argList.get(0);
} else {
return argList;
}
}
}
5.定义操作枚举类
package com.efiles.common.enums;
/**
* @ClassName OperationType
* @Author Administrator
* @Date 2020/8/27 0027 下午 2:05
**/
public enum OperationType {
/**
* 操作类型
*/
UNKNOWN("未知"),
DELETE("删除"),
SELECT("查询"),
UPDATE("修改"),
INSERT("添加");
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
OperationType(String value){
this.value=value;
}
}
package com.efiles.common.enums;
/**
* @ClassName LoginType
* @Author Administrator
* @Date 2020/8/27 0027 下午 2:51
**/
public enum LoginType {
UNKNOWN("未知"),
LOGIN("登录"),
LOGINOUT("退出");
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
LoginType(String value){
this.value=value;
}
}
6.自定义注解类
package com.efiles.common.annotation;
import com.efiles.common.enums.LoginType;
import com.efiles.common.enums.OperationType;
import java.lang.annotation.*;
/**
* @author
*
* @Retention 注解会在class中存在,运行时可通过反射获取
* @Target//目标是方法
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BusinessLog {
/**
* 操作类型(enum):主要是select,insert,update,delete
*/
OperationType operationType() default OperationType.UNKNOWN;
/**
* 登录操作(enum):主要是login,loginout
*/
LoginType loginType() default LoginType.UNKNOWN;
/**
* 方法具体操作
*/
String methodDetail() default "";
}
7.在方法上加上注解
@BusinessLog(methodDetail = "首页",loginType = LoginType.LOGIN)
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
public CommonResult login() {
return CommonResult.success(null);
}