spring中的aop实现各个类方法的日志拦截

Posted 阿啄debugIT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring中的aop实现各个类方法的日志拦截相关的知识,希望对你有一定的参考价值。

spring中的aop实现各个类方法的日志拦截。

1、编写SysLogAspect类

@Slf4j
@Aspect
@Configuration
public class SysLogAspect 
    private static final Logger logger = LoggerFactory.getLogger(SysLogAspect.class);


    @Around("@annotation(apiOperation)")
    public Object around(ProceedingJoinPoint point,ApiOperation apiOperation) throws Throwable 
        String strClassName = point.getTarget().getClass().getName();
        String strMethodName = point.getSignature().getName();
        logger.info("[类名]:,[方法]:",strClassName, strMethodName);
        logger.info("[tags]:,[value]:,[notes]:", apiOperation.tags(),apiOperation.value(),apiOperation.notes());
        Object obj = point.proceed();
        //发送异步日志事件
        return obj;
    

看到了 around(ProceedingJoinPoint point,ApiOperation apiOperation)方法中的ApiOperation,来源于io.swagger.annotations

2、横切其他业务类的具体方法,实现业务方法操作的日志监控

3、具体例子

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.*;

/**
 * <b><Id>ParamSettingController</Id></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2019/12/8 15:22.
 *
 */
@SuppressWarnings("MagicConstant")
@RestController
@RequestMapping("/v1.0")
@Api(value = "[Gpile-voiler 1.0]参数设置")
public class ParamSettingController 

    /**
     * logger.
     */
    private static Logger logger = LoggerFactory.getLogger(ParamSettingController.class);

    /**
     * The G param setting service.
     */
    @Autowired
    private GParamSettingService GParamSettingService;

    /**
     * get alert result.
     */
    @RequestMapping(value = "/param/setting", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "[Gpile-flower]参数设置", notes = "get flower alert result")
    @ApiResponses(value = 
            @ApiResponse(Id = 200, message = "successful query", response = Integer.class, responseContainer = "int"),
            @ApiResponse(Id = 500, message = "internal server error") )
    public ResponseResult getParamSettingResult(
            @ApiParam(value = "请求时间:时间戳格式",required = true) @RequestParam(value = "time") Long time,
            @ApiParam(value = "设备编号,APPLLO:120",required = true) @RequestParam(value = "deviceId") Long deviceId,
            @ApiParam(value = "应用编码:flower_dongjin",required = true) @RequestParam(value = "appId") String appId,
            @ApiParam(value = "模块标志:paramSetting",required = true) @RequestParam(value = "modual") String modual,
            @ApiParam(value = "维度,APPLLO:120;BPPLLO:130",required = true) @RequestParam(value = "dimension") String dimension
            ) 
			
        return ResponseResult.ok(GParamSettingService.query(deviceId,appId,modual,dimension,time));
           

特别:@ApiOperation(value = "[Gpile-flower]参数设置", notes = "get flower alert result")

以上是关于spring中的aop实现各个类方法的日志拦截的主要内容,如果未能解决你的问题,请参考以下文章

spring aop无法拦截类内部的方法调用

Spring AOP中@Aspect拦截介绍(二)

Spring AOP 实现日志记录功能

SSH 下做一个spring AOP的 操作日志记录功能

AOP实现日志怎么做啊?

Spring aop 拦截不到Dao