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")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章