小熊科技|从零学ELK系列(十):SpringBoot接入ELK升级版( 二 )

<>();if (argNames != nulli < argNames.length; i++) {String thisArgName = argNames[i];String thisArgValue = http://kandian.youth.cn/index/argValues[i].toString();linkedHashMap.put(thisArgName, thisArgValue);}}return JSON.toJSONString(linkedHashMap);}public static String getStringTodayTime() {Date todat_date = new Date();//将日期格式化SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//转换成字符串格式return simpleDateFormat.format(todat_date);}}

  • RequestLogAspectConf
/** Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.* 项目名称:SpringBoot项目接入ELK* 类名称:RequestLogAspectConf.java* 创建人:张晗* 联系方式:zhanghan_java@163.com* 开源地址:* 博客地址:*/package com.zhanghan.zhelkboot.aop;import com.zhanghan.zhelkboot.util.FileBeatLogUtil;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.beans.factory.annotation.Autowired;import org.springframework.core.annotation.Order;import org.springframework.core.env.Environment;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;@Aspect@Order(0)@Componentpublic class RequestLogAspectConf {@Autowiredprivate HttpServletRequest request;@Autowiredprivate Environment env;private final Logger logger = LoggerFactory.getLogger(this.getClass());/*** 范围切点方法*/@Pointcut("execution(* com.zhanghan.zhelkboot.controller..*.*(..))")public void methodPointCut() {}@Before("methodPointCut()")void doBefore(JoinPoint joinPoint) {authLogic(joinPoint);}private void authLogic(JoinPoint joinPoint) {try {String applicationName = env.getProperty("spring.application.name");//获取当前http请求String reqName = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();String requestParams = FileBeatLogUtil.getParams(joinPoint);FileBeatLogUtil.writeRequestInfo(request, applicationName, reqName, requestParams);} catch (Exception e) {logger.error("authLogic;Exception:{}", e.getMessage());}}}
  • ResponseLogAdvice
/** Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.* 项目名称:SpringBoot项目接入ELK* 类名称:ResponseLogAdvice.java* 创建人:张晗* 联系方式:zhanghan_java@163.com* 开源地址:* 博客地址:*/package com.zhanghan.zhelkboot.aop;import com.zhanghan.zhelkboot.util.FileBeatLogUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.MethodParameter;import org.springframework.http.MediaType;import org.springframework.http.server.ServerHttpRequest;import org.springframework.http.server.ServerHttpResponse;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import javax.servlet.http.HttpServletResponse;@ControllerAdvicepublic class ResponseLogAdvice implements ResponseBodyAdvice {@Autowiredprivate HttpServletResponse response;private final Logger logger = LoggerFactory.getLogger(this.getClass());@Overridepublic boolean supports(MethodParameter methodParameter, Class aClass) {return true;}@Overridepublic Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {try {if (o != null) {Logger log = LoggerFactory.getLogger("logstashInfo");FileBeatLogUtil.writeResponseLog(o, log, response);}} catch (Exception e) {logger.error("beforeBodyWrite;Exception:{}", e.getMessage());}return o;}}