mysql aspect_基于springboot+semantic-ui+mysql+thymeleaf开发小而美的个人博客网站

package com.lrm.aspect;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.Arrays;

/**

* Created by limi on 2017/10/13.

*/

@Aspect

@Component

public class LogAspect {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Pointcut("execution(* com.lrm.web.*.*(..))")

public void log() {}

@Before("log()")

public void doBefore(JoinPoint joinPoint) {

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = attributes.getRequest();

String url = request.getRequestURL().toString();

String ip = request.getRemoteAddr();

String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();

Object[] args = joinPoint.getArgs();

RequestLog requestLog = new RequestLog(url, ip, classMethod, args);

logger.info("Request : {}", requestLog);

}

@After("log()")

public void doAfter() {

// logger.info("--------doAfter--------");

}

@AfterReturning(returning = "result",pointcut = "log()")

public void doAfterRuturn(Object result) {

logger.info("Result : {}", result);

}

private class RequestLog {

private String url;

private String ip;

private String classMethod;

private Object[] args;

public RequestLog(String url, String ip, String classMethod, Object[] args) {

this.url = url;

this.ip = ip;

this.classMethod = classMethod;

this.args = args;

}

@Override

public String toString() {

return "{" +

"url='" + url + '\'' +

", ip='" + ip + '\'' +

", classMethod='" + classMethod + '\'' +

", args=" + Arrays.toString(args) +

'}';

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值