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

【从零学ELK系列目录】从零学ELK系列(一):为什么要跟我学从零学ELK系列
从零学ELK系列(二):VMware安装Centos(超详细图文教程)
从零学ELK系列(三):Centos安装Docker(超详细图文教程)
从零学ELK系列(四)Docker安装Elasticsearch(超详细图文教程)
从零学ELK系列(五):Docker安装kibana(超详细图文教程)
从零学ELK系列(六):Docker安装Logstash(超详细图文教程)
从零学ELK系列(七):Centos安装Filebeat(超详细图文教程)
从零学ELK系列(八):SpringBoot项目接入ELK(超详细图文教程)
从零学ELK系列(九):Nginx接入ELK(超详细图文教程)
从零学ELK系列(十):SpringBoot项目接入ELK升级版(超详细图文教程)
前言之前在《从零学ELK系列(八):SpringBoot项目接入ELK(超详细图文教程) 》中演示了SpringBoot项目接入ELK , 后来项目中对这部分进行了优化 , 之前博文中也有读者问到 , 将优化整理成博文和大家共享;
优化前:
一次请求记录两条日志(request一条 , response一条) , 通过UUID传连起来
优化后:
一次请求记录一条日志(request信息与response信息都在一起)
架构图import com.alibaba.fastjson.JSON;import com.zhanghan.zhelkboot.util.wrapper.Wrapper;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.reflect.MethodSignature;import org.slf4j.Logger;import org.slf4j.MDC;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.text.SimpleDateFormat;import java.util.*;public class FileBeatLogUtil {public static void writeRequestInfo(HttpServletRequest request, String applicationName, String reqName, String requestParams) {String requestURI = request.getRequestURI();//获取requestHeaderEnumeration requestHeaderNames = request.getHeaderNames();Map reuestHeaderMap = new HashMap<>();while (requestHeaderNames.hasMoreElements()) {String name = requestHeaderNames.nextElement();String value = http://kandian.youth.cn/index/request.getHeaders(name).nextElement();reuestHeaderMap.put(name, value);}String requestHeader ="";if (null != reuestHeaderMap}//防止MDC值空指针 , 所有入参不为nullapplicationName = org.springframework.util.StringUtils.isEmpty(applicationName) ? "" : applicationName;requestURI = org.springframework.util.StringUtils.isEmpty(requestURI) ? "" : requestURI;reqName = org.springframework.util.StringUtils.isEmpty(reqName) ? "" : reqName;requestParams = "null".equals(requestParams) ? "" : requestParams;//MDC值为ES键值对JSON信息MDC.put("applicationName", applicationName);MDC.put("requestTime", getStringTodayTime());MDC.put("requestURI", requestURI);MDC.put("requestHeader", requestHeader);MDC.put("sourceName", reqName);MDC.put("requestParams", requestParams);}public static void writeResponseLog(Object o, Logger log, HttpServletResponse response) {//取responseHeader内容Map responseHeaderMap = new HashMap<>();Collection headerNames = response.getHeaderNames();headerNames.forEach(name -> {responseHeaderMap.put(name, response.getHeader(name));});String strResponseHeader = "";if (null != responseHeaderMap}//获取response内容String responseCode = "";String responseMsg = "";String responseBody = "";Wrapper wrapper;if (null != o) {wrapper = (Wrapper) o;if (null != wrapper) {responseCode = String.valueOf(wrapper.getCode());responseMsg = wrapper.getMessage();responseBody = wrapper.getResult().toString();}}//MDC值为ES键值对JSON信息MDC.put("responseHeader", strResponseHeader);MDC.put("responseCode", responseCode);MDC.put("responseMsg", responseMsg);MDC.put("responseBody", responseBody);MDC.put("responseTime", getStringTodayTime());Map copyOfContextMap = MDC.getCopyOfContextMap();String reqInfoJsonStr = JSON.toJSONString(copyOfContextMap);log.info(reqInfoJsonStr);}/*** 获取请求参数 , 处理为json字符串** @param joinPoint* @return*/public static String getParams(JoinPoint joinPoint) {Object[] argValues = joinPoint.getArgs();String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();LinkedHashMap linkedHashMap = new LinkedHashMap