JAVA統一介面日誌切面列印方法引數
package com.gaara.musicscore.config.log;
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
@Aspect
@Component
public class WebLogAspect {
private Logger logger = LoggerFactory.getLogger(getClass());
private final String httpStartTime = "HTTP_START_TIME";
@Pointcut("execution(public * com.gaara.musicscore.api..*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
try {
if (!logger.isDebugEnabled()) {
return;
}
// 接收到請求,記錄請求內容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
request.setAttribute(httpStartTime, System.currentTimeMillis());
StringBuilder sb = new StringBuilder(1000);
sb.append("\n-----------------------------開始呼叫:-----------------------------\n");
sb.append("Controller: ").append(joinPoint.getSignature().getDeclaringType()).append("\n");
sb.append("Method : ").append(joinPoint.getSignature().getName()).append("\n");
sb.append("Header : {").append("\n");
Enumeration<String> requestHeader = request.getHeaderNames();
while (requestHeader.hasMoreElements()) {
String headerKey = requestHeader.nextElement();
// 列印所有Header值
sb.append(" ").append(headerKey).append(" : ").append(request.getHeader(headerKey)).append("\n");
}
sb.append("}").append("\n");
sb.append("Params : ").append(getParams(joinPoint)).append("\n");
sb.append("URI : ").append(request.getMethod()).append(" ").append(request.getRequestURI()).append("\n");
sb.append("-----------------------------開始呼叫:-----------------------------");
// 記錄下請求內容
logger.debug(sb.toString());
} catch (Exception e) {
logger.warn("切面處理前錯誤", e);
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {
try {
if (!logger.isDebugEnabled()) {
return;
}
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
long ms = System.currentTimeMillis() - (long) request.getAttribute(httpStartTime);
// 處理完請求,返回內容
StringBuilder sb = new StringBuilder(50);
sb.append("\n-----------------------------結束呼叫:-----------------------------\n");
sb.append("Controller: ").append(joinPoint.getSignature().getDeclaringType()).append("\n");
sb.append("URI : ").append(request.getMethod()).append(" ").append(request.getRequestURI()).append("\n");
sb.append("Method : ").append(joinPoint.getSignature().getName()).append("\n");
sb.append("return : ").append(JSON.toJSONString(ret)).append("\n");
sb.append("CostTime : ").append(ms).append("ms\n");
sb.append("-----------------------------結束呼叫:-----------------------------");
logger.debug(sb.toString());
} catch (Exception e) {
logger.warn("切面處理後錯誤", e);
}
}
private StringBuilder getParams(JoinPoint joinPoint) {
StringBuilder params = new StringBuilder();
for (Object arg : joinPoint.getArgs()) {
try {
if (!(arg instanceof ServletRequest)
&& !(arg instanceof ServletResponse)
&& !(arg instanceof MultipartFile)
&& !(arg instanceof ModelAndView)
&& !(arg instanceof Model)
) {
params.append(", ");
params.append(JSON.toJSONString(arg));
}
} catch (Exception e) {
logger.warn("未知型別轉換錯誤:" + arg.getClass().getSimpleName());
}
}
return params;
}
}
相關文章
- Spring Boot 2.x 教程系列 | AOP 切面統一列印請求日誌Spring Boot
- 如何列印完整的MYSQL帶引數SQL日誌資訊MySql
- java日誌列印使用指南Java
- Jenkins:引數化構建:分支|模組|回滾|列印日誌Jenkins
- 【Spring】日誌列印sql,日誌配置列印sqlSpringSQL
- 【工具】方法日誌列印+任務切片
- 日誌列印
- [Java/日誌] 日誌框架列印應用程式日誌程式碼的執行情況Java框架
- python logger 列印日誌錯誤行數Python
- java 日誌脫敏框架 sensitive,優雅的列印脫敏日誌Java框架
- 日誌--列印規範
- Log 工具列印日誌
- Unity——日誌列印工具Unity
- MySQL日誌引數配置簡介MySql
- 如何優雅地在 Spring Boot 中使用自定義註解,AOP 切面統一列印出入參日誌 | 修訂版Spring Boot
- 在Java程式中列印java執行時引數Java
- 39,日誌列印sql 配置SQL
- feign配置日誌不列印
- MySQL列印死鎖日誌MySql
- 日誌到底該如何列印?
- Open Feign使用方法、等待時間、日誌列印功能
- 簡單介紹MySQL列印死鎖日誌的方法MySql
- Java專案如何統一日誌管理Java
- Android進階:一、日誌列印和儲存策略Android
- Linux動態列印kernel日誌Linux
- 設定mybatis 是否列印日誌MyBatis
- SwiftUI 如何在介面中print列印日誌log (EmptyView教程含原始碼)SwiftUIView原始碼
- Logstash 配置Java日誌格式的方法Java
- Java 面試題之 Logback 列印日誌是如何獲取當前方法名稱的?Java面試題
- SpringBoot專案實現日誌列印SQL明細(包括SQL語句和引數)幾種方式Spring BootSQL
- 一個Java方法能使用多少個引數?Java
- Spring MVC結合日誌框架給一次請求日誌列印出唯一標示SpringMVC框架
- Win10系統中使用“系統配置”啟用“引導日誌”的方法Win10
- 另一種列印輸出日誌資訊的方式
- Flutter - 列印好用的Debug日誌Flutter
- 統一日誌管理
- linux 檢視日誌的命令引數有哪些?Linux
- dg庫日誌應用慢引數調整