elasticsearch整合springboot做日志收集功能

目标:通过注解的方式对接口进行日志记录,日志保存在es中

本文不讲如何安装es了,最简单的用于测试的话可以直接docker启动一个

  1. 先创建一个自定义注解类 SysLog
 /**
     * 模块 
     */
    public String title() default "";

    /**
     * 功能
     */
    public BusinessType businessType() default BusinessType.OTHER;

    /**
     * 操作人类别
     */
    public OperatorType operatorType() default OperatorType.MANAGE;

    /**
     * 是否保存请求的参数
     */
    public boolean isSaveRequestData() default true;
  1. 编写切面类
private static final Logger log = LoggerFactory.getLogger(SysLogAspect.class);

    @Autowired
    private OperationLogDao operationLogDao;

    /**
     * 配置织入点
     *
     */
    @Pointcut("@annotation(com.aiyuncar.btcp.common.eslog.annotation.SysLog)")
    public void logPointCut()
    {
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult)
    {
        handleLog(joinPoint, null, jsonResult);
    }

    /**
     * 拦截异常操作
     * 
     * @param joinPoint 切点
     * @param e 异常
     */
    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e)
    {
        handleLog(joinPoint, e, null);
    }

    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult)
    {
        try
        {
            // 获得注解
            SysLog controllerLog = getAnnotationLog(joinPoint);
            if (controllerLog == null)
            {
                return;
            }

            // 获取当前的用户
            SysUserBO authUser = SysUserHelper.getAuthUser();

            // *========数据库日志=========*//
            OperationLog operLog = new OperationLog();
            operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
            // 返回参数
            operLog.setJsonResult(JSON.toJSONString(jsonResult));

            operLog.setOperUrl(ServletContextUtils.getRequest().getRequestURI());
            if (authUser != null)
            {
                operLog.setOperName(authUser.getUserName());
                operLog.setDeptName(authUser.getDept().getDeptName());
            }

            if (e != null)
            {
                operLog.setStatus(BusinessStatus.FAIL.ordinal());
                operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
            }
            // 设置方法名称
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            operLog.setMethod(className + "." + methodName + "()");
            // 设置请求方式
            operLog.setRequestMethod(ServletContextUtils.getRequest().getMethod());
            // 处理设置注解上的参数
            getControllerMethodDescription(joinPoint, controllerLog, operLog);
            operLog.setId(UUID.randomUUID().toString());
            Date sqlDate = new java.sql.Date(System.currentTimeMillis());
            operLog.setOperTime(sqlDate);
            operLog.setOperTimeTimeStamp(System.currentTimeMillis());
            // 保存到 ES 中
            operationLogDao.save(operLog);
        }
        catch (Exception exp)
        {
            // 记录本地异常日志
            log.error("==前置通知异常==");
            log.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     * 
     * @param log 日志
     * @param operLog 操作日志
     * @throws Exception
     */
    public void getControllerMethodDescription(JoinPoint joinPoint, SysLog log, OperationLog operLog) throws Exception
    {
        // 设置action动作
        operLog.setBusinessType(log.businessType().ordinal());
        // 设置标题
        operLog.setTitle(log.title());
        // 设置操作人类别
        operLog.setOperatorType(log.operatorType().ordinal());
        // 是否需要保存request,参数和值
        if (log.isSaveRequestData())
        {
            // 获取参数的信息,传入到数据库中。
            setRequestValue(joinPoint, operLog);
        }
    }

    /**
     * 获取请求的参数,放到log中
     * 
     * @param operLog 操作日志
     * @throws Exception 异常
     */
    private void setRequestValue(JoinPoint joinPoint, OperationLog operLog) throws Exception
    {
        String requestMethod = operLog.getRequestMethod();
        if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
        {
            String params = argsArrayToString(joinPoint.getArgs());
            operLog.setOperParam(StringUtils.substring(params, 0, 2000));
        }
        else
        {
            Map<?, ?> paramsMap = (Map<?, ?>) ServletContextUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
            operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
        }
    }

    /**
     * 是否存在注解,如果存在就获取
     */
    private SysLog getAnnotationLog(JoinPoint joinPoint) throws Exception
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(SysLog.class);
        }
        return null;
    }

    /**
     * 参数拼装
     */
    private String argsArrayToString(Object[] paramsArray)
    {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0)
        {
            for (int i = 0; i < paramsArray.length; i++)
            {
                if (!isFilterObject(paramsArray[i]))
                {
                    Object jsonObj = JSON.toJSON(paramsArray[i]);
                    params += jsonObj.toString() + " ";
                }
            }
        }
        return params.trim();
    }

    /**
     * 判断是否需要过滤的对象。
     * 
     * @param o 对象信息。
     * @return 如果是需要过滤的对象,则返回true;否则返回false。
     */
    public boolean isFilterObject(final Object o)
    {
        return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse;
    }

  1. springboot中引入elasticsearch
    (1)pom 包引入
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

(2)dao

public interface OperationLogDao extends ElasticsearchRepository<OperationLog, String> {

}

(3)po 类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "systemlogger", type = "operationlog")
private static final long serialVersionUID = 1L;

    @Id
    private String id;

    /*操作模块*/
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;

    /*业务类型 0=其它,1=新增,2=修改,3=删除,4=授权,5=导出,6=导入,7=强退,8=生成代码,9=清空数据*/
//    @Field(type = FieldType.Keyword)
    private Integer businessType;

    /** 业务类型数组 */
    private Integer[] businessTypes;

    /** 请求方法 */
    private String method;

    /** 请求方式 */
    private String requestMethod;

    /** 操作类别(0其它 1后台用户 2手机端用户) */
    private Integer operatorType;

    /** 操作人员 */
    private String operName;

    /** 部门名称 */
    private String deptName;

    /** 请求url */
    private String operUrl;

    /** 请求参数 */
    private String operParam;

    /** 返回参数 */
    private String jsonResult;

    /** 操作状态(0正常 1异常) */
    private Integer status;

    /** 错误消息 */
    private String errorMsg;

    /** 操作时间 yyyy-MM-dd HH:mm:ss */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date operTime;

    /** 操作时间戳 */
    private Long operTimeTimeStamp;

(4)controller展示层

@GetMapping("/v1/{id}")
    @ApiOperation(value = "根据编号获取详细信息", notes = "根据编号获取详细信息",response = OperationResponseResult.OperationLogResponseResult.class)
    public OperationLogVO findById(@PathVariable String id) {
        if (!StrUtil.isEmpty(id)) {
            Optional<OperationLog> operationLog = operationLogDao.findById(id);
            if (operationLog.isPresent()){
                OperationLogVO logVO = new OperationLogVO();
                OperationLog operationLogpo = operationLog.get();
                BeanUtils.copyProperties(operationLogpo,logVO);
                return logVO;
            }
        }
        return null;
    }

    /**
     * 全文搜索
     * @param keyword 关键字
     */
    @GetMapping("/v1/full")
    @ApiOperation(value = "全文搜索", notes = "全文搜索")
    public PageVO<OperationLog> full(String keyword, QueryQO query) {
        PageVO pageVO = new PageVO();
        // 构造分页类
        Pageable pageable = PageRequest.of(query.getPage()-1, query.getPageSize());
        // 构造查询 NativeSearchQueryBuilder
        NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder()
                .withPageable(pageable)
                ;
        if (!StrUtil.isEmpty(keyword)) {
            searchQueryBuilder.withQuery(QueryBuilders.queryStringQuery(keyword));
        }

        SearchQuery searchQuery = searchQueryBuilder.build();
        Page<OperationLog> phoneModelPage = elasticsearchTemplate.queryForPage(searchQuery, OperationLog.class);

        pageVO.setList(phoneModelPage.getContent());
        pageVO.setTotal((int)phoneModelPage.getTotalElements());
        pageVO.setPage(phoneModelPage.getNumber()+1);
        pageVO.setPages(phoneModelPage.getTotalPages());
        pageVO.setSize(phoneModelPage.getNumberOfElements());
        pageVO.setPageSize(phoneModelPage.getSize());

        return pageVO;
    }

    /**
     * 高级搜索,根据字段进行搜索
     * @return {@link Page<OperationLog>}
     */
    @GetMapping("/v1/heightSearch")
    @ApiOperation(value = "高级搜索,根据字段进行搜索", notes = "高级搜索,根据字段进行搜索")
    public PageVO<OperationLog> search(OperationLogQO operationLogQO,QueryQO query) {
        PageVO pageVO = new PageVO();
        // 构造分页对象
        Pageable pageable = PageRequest.of(query.getPage()-1, query.getPageSize());
        // BoolQueryBuilder (Elasticsearch Query)
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (!StringUtils.isEmpty(operationLogQO.getBusinessType())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("businessType", operationLogQO.getBusinessType()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getOperName())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("operName", operationLogQO.getOperName()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getOperatorType())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("operatorType", operationLogQO.getOperatorType()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getStatus())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("status", operationLogQO.getStatus()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getRequestMethod())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("requestMethod", operationLogQO.getRequestMethod()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getTitle())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("title", operationLogQO.getTitle()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getStart())) {
            Date startTime = null;
            try {
                SimpleDateFormat formatter  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                startTime = formatter.parse(operationLogQO.getStart());
            } catch (Exception e) {
                e.printStackTrace();
            }
            boolQueryBuilder.must(QueryBuilders.rangeQuery("operTimeTimeStamp").gte(startTime.getTime()));
//            boolQueryBuilder.must(QueryBuilders.rangeQuery("operTime").gte(startTime.getTime()));
        }

        if (!StringUtils.isEmpty(operationLogQO.getEnd())) {
            Date endTime = null;
            try {
                SimpleDateFormat formatter  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                endTime = formatter.parse(operationLogQO.getEnd());
            } catch (Exception e) {
                e.printStackTrace();
            }
            boolQueryBuilder.must(QueryBuilders.rangeQuery("operTimeTimeStamp").lte(endTime.getTime()));
//            boolQueryBuilder.must(QueryBuilders.rangeQuery("operTime").lte(endTime.getTime()));
        }

        // BoolQueryBuilder (Spring Query)
        Sort sort = new Sort(Sort.Direction.DESC, "operTimeTimeStamp");
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(boolQueryBuilder)
                .build()
                .addSort(sort)
                ;

        Page<OperationLog> phoneModelPage = elasticsearchTemplate.queryForPage(searchQuery, OperationLog.class);

        pageVO.setList(phoneModelPage.getContent());
        pageVO.setTotal((int)phoneModelPage.getTotalElements());
        pageVO.setPage(phoneModelPage.getNumber()+1);
        pageVO.setPages(phoneModelPage.getTotalPages());
        pageVO.setSize(phoneModelPage.getNumberOfElements());
        pageVO.setPageSize(phoneModelPage.getSize());
        return pageVO;
    }

(5)使用,在需要日志记录的接口中加入注解即可

@SysLog(title = "部门管理", businessType = BusinessType.INSERT)

(6)其中SysUserHelper是获取当前用户信息的工具类,这个需要替换为你项目中对应的工具类

评论

渣男 : 456
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×