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;
}
}
相關文章
- 如何列印完整的MYSQL帶引數SQL日誌資訊MySql
- Spring Boot 2.x 教程系列 | AOP 切面統一列印請求日誌Spring Boot
- java日誌列印使用指南Java
- Jenkins:引數化構建:分支|模組|回滾|列印日誌Jenkins
- 【工具】方法日誌列印+任務切片
- [Java/日誌] 日誌框架列印應用程式日誌程式碼的執行情況Java框架
- java 日誌脫敏框架 sensitive,優雅的列印脫敏日誌Java框架
- Unity——日誌列印工具Unity
- 日誌--列印規範
- Log 工具列印日誌
- MySQL日誌引數配置簡介MySql
- EBS相關日誌和引數
- 在Java程式中列印java執行時引數Java
- 39,日誌列印sql 配置SQL
- MySQL列印死鎖日誌MySql
- 日誌到底該如何列印?
- mybatis列印sql日誌配置MyBatisSQL
- 如何優雅地在 Spring Boot 中使用自定義註解,AOP 切面統一列印出入參日誌 | 修訂版Spring Boot
- 簡單介紹MySQL列印死鎖日誌的方法MySql
- jvm-All日誌啟動引數JVM
- python logger 列印日誌錯誤行數Python
- 另一種列印輸出日誌資訊的方式
- feign配置日誌不列印
- Tornado按天列印日誌
- 設定mybatis 是否列印日誌MyBatis
- SwiftUI 如何在介面中print列印日誌log (EmptyView教程含原始碼)SwiftUIView原始碼
- Open Feign使用方法、等待時間、日誌列印功能
- 歸檔日誌命令及引數總結
- 透過alert日誌重建引數檔案
- mysql的日誌引數修改的問題.MySql
- 一個Java方法能使用多少個引數?Java
- Win10系統開啟引導日誌的兩個方法Win10
- Android進階:一、日誌列印和儲存策略Android
- Linux動態列印kernel日誌Linux
- 【MySQL】慢查詢日誌不列印MySql
- 統一日誌管理
- Win10系統中使用“系統配置”啟用“引導日誌”的方法Win10
- Rust中獲得CPU核數與列印日誌到控制檯?Rust