牛客网项目17:统一记录日志

1. 大体思路

记录日志可能发生在任何时刻,为了方便管理、修改,我们采用AOP面向方面(切面)编程。

③ 

2. 代码

① 引入依赖

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.18</version>
        </dependency>

② 使用示例

在com.nowcoder.mycommunity目录下新建包:aspect,并在里面新建类:AlpahAspect

package com.nowcoder.mycommunity.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 仅作为示例!!!
 */

//@Component
//@Aspect //表示这是一个增强功能的实现
public class AlpahAspect {
    //执行顺序:
    //aroundbefore、before、aroundafter、after、afterRetuning、around before、before

    @Pointcut("execution(* com.nowcoder.mycommunity.service.*.*(..))")
    //切入点表达式,表示在哪一个包下的哪一个方法进行功能增强
    //任意形式的返回值、包下所有组件、所有方法、所有参数
    public void pointCut(){

    }

    //表示以此为目标对象执行前
    @Before("pointCut()")
    public void before(){
        System.out.println("before");
    }

    //表示以此为目标对象执行后
    @After("pointCut()")
    public void after(){
        System.out.println("after");
    }

    //前后都植入逻辑
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        //proceedingJoinPoint表示连接点
        //这样可以在目标方法前后,做一些逻辑处理,把原来方法的执行夹在中间
        System.out.println("aroundbefore");
        Object obj = joinPoint.proceed();  //调用目标对象被处理的方法
        System.out.println("aroundafter");
        //在这里可以执行代理对象执行之后做什么事情
        return obj;
    }

    //表示以此为抛出异常的时候
    @AfterThrowing("pointCut()")
    public void afterThrow(){
        System.out.println("afterThrow");
    }

    //表示以此为有了返回值以后
    @AfterReturning("pointCut()")
    public void AfterReturning(){
        System.out.println(" AfterReturning");
    }
}

③ 记录日志功能

package com.nowcoder.mycommunity.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
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.text.SimpleDateFormat;
import java.util.Date;

/**
 * 此类作为业务功能增强。实现日志的功能
 */
@Component
@Aspect
public class ServiceLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);

    @Pointcut("execution(* com.nowcoder.mycommunity.service.*.*(..))")//切入点表达式,表示在哪一个包下的哪一个方法进行功能增强
    public void pointCut(){

    }

    /**
     * 定义好切入点表达式,在这里实现业务功能增强,选取连接点,定义哪个方法上边生效
     */
    @Before("pointCut()")
    public void before(JoinPoint joinPoint){//用来获取连接点的位置
        //用户[1,2,3,4],在[xxx]访问了[com.nowcoder.community.service]
        //获取用户ip利用工具类 RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (requestAttributes==null){
           return;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        //获取ip
        String ip = request.getRemoteHost();
        //获取时间
        String now = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
        //目标的类型名 + 方法名
        String target = joinPoint.getSignature().getDeclaringTypeName() + "." +joinPoint.getSignature().getName();
        logger.info(String.format("用户[%s],在[%s],访问了[%s]", ip, now, target));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值