主要程式碼展示
/*
* Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
* 專案名稱:SpringBoot專案接入ELK
* 類名稱:ExceptionLogAspect.java
* 建立人:張晗
* 聯絡方式:zhanghan_java@163.com
* 開源地址:
* 部落格地址: https://zhanghan.blog.csdn.net
*/package com.zhanghan.zhelkboot.aop;import com.zhanghan.zhelkboot.util.FileBeatLogUtil;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.util.Arrays;@Aspect@Order(1)@Componentpublic class ExceptionLogAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 範圍切點方法
*/
@Pointcut("execution(* com.zhanghan.zhelkboot..*.*(..))")
public void methodPointCut() {
}
@AfterThrowing(throwing = "ex", pointcut = "methodPointCut()")
public void throwss(JoinPoint joinPoint, Exception ex) {
try {
String methodArgs = Arrays.toString(joinPoint.getArgs());
FileBeatLogUtil.writeExceptionLog(joinPoint.getSignature().toString(), methodArgs, ex.getMessage());
} catch (Exception e) {
logger.error("ExceptionLogAspect;writeExceptionLog;Exception:{}", e.getMessage());
}
}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
- RecordExcepitonLogServiceImpl
/*
* Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
* 專案名稱:SpringBoot專案接入ELK
* 類名稱:RecordExcepitonLogServiceImpl.java
* 建立人:張晗
* 聯絡方式:zhanghan_java@163.com
* 開源地址:
* 部落格地址: https://zhanghan.blog.csdn.net
*/package com.zhanghan.zhelkboot.service.impl;import com.zhanghan.zhelkboot.controller.request.RecordExceptionLogRequest;import com.zhanghan.zhelkboot.service.RecordExcepitonLogService;import com.zhanghan.zhelkboot.util.wrapper.WrapMapper;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.Map;@Servicepublic class RecordExcepitonLogServiceImpl implements RecordExcepitonLogService {
/**
* 記錄異常
*/
@Override
public Object recordExcepitonLog(RecordExceptionLogRequest recordExceptionLogRequest) {
Integer divisor = recordExceptionLogRequest.getDivisor();
int consult = 2 / divisor;
Map<String, Object> map = new HashMap();
map.put("consult", consult);
return WrapMapper.ok(map);
}}123456789101112131415161718192021222324252627282930313233343536
/*
* Copyright (c) 2020. zhanghan_java@163.com All Rights Reserved.
* 專案名稱:SpringBoot專案接入ELK
* 類名稱:FileBeatLogUtil.java
* 建立人:張晗
* 聯絡方式:zhanghan_java@163.com
* 開源地址:
* 部落格地址: https://zhanghan.blog.csdn.net
*/package com.zhanghan.zhelkboot.util;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 org.springframework.util.StringUtils;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();
//獲取requestHeader
Enumeration<String> requestHeaderNames = request.getHeaderNames();
Map<String, Object> reuestHeaderMap = new HashMap<>();
while (requestHeaderNames.hasMoreElements()) {
String name = requestHeaderNames.nextElement();
String value = request.getHeaders(name).nextElement();
reuestHeaderMap.put(name, value);
}
String requestHeader = "";
if (null != reuestHeaderMap && reuestHeaderMap.size() > 0) {
requestHeader = JSON.toJSONString(reuestHeaderMap);
}
//防止MDC值空指標,所有入參不為null
applicationName = StringUtils.isEmpty(applicationName) ? "" : applicationName;
requestURI = StringUtils.isEmpty(requestURI) ? "" : requestURI;
reqName = 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);
MDC.put("exceptionCount", "0");
}
public static void writeExceptionLog(String exceptionMethodName, String exceptionMethodArgs, String exceptionMessage) {
MDC.put("exceptionCount", "1");
exceptionMessage = String.format("MethodName:%s;Args:%s;Exception:%s", exceptionMethodName, exceptionMethodArgs, exceptionMessage);
//MDC值為ES鍵值對JSON資訊
MDC.put("exceptionMessage", exceptionMessage);
}
public static void writeResponseLog(Object o, Logger log, HttpServletResponse response) {
//取responseHeader內容
Map<String, Object> responseHeaderMap = new HashMap<>();
Collection<String> headerNames = response.getHeaderNames();
headerNames.forEach(name -> {
responseHeaderMap.put(name, response.getHeader(name));
});
String strResponseHeader = "";
if (null != responseHeaderMap && responseHeaderMap.size() > 0) {
strResponseHeader = JSON.toJSONString(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();
Object result = wrapper.getResult();
if (null != result) {
responseBody = result.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<String, String> 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<String, Object> linkedHashMap = new LinkedHashMap<>();
if (argNames != null && argNames.length > 0) {
for (int i = 0; i < argNames.length; i++) {
String thisArgName = argNames[i];
String thisArgValue = 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);
}}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141