springboot利用@Aspect實現(xiàn)日志工具類的詳細(xì)代碼
更新時間:2022年03月21日 14:47:21 作者:java-zh
這篇文章主要介紹了springboot利用@Aspect實現(xiàn)日志工具類,通過實例代碼介紹了導(dǎo)包及在啟動類上進行注解自動掃描的方法,需要的朋友可以參考下
一、導(dǎo)包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>二、在啟動類上進行注解自動掃描
@SpringBootApplication
@EnableCaching
@EnableAsync
@ComponentScan({"com.workorder.*"})
public class WorkOrderApplication {
public static void main(String[] args) {
SpringApplication.run(WorkOrderApplication.class, args);
}
}三、工具類
@Aspect
@Component
@Slf4j
public class LogAspect {
@Autowired
private Environment env;
@Pointcut("execution(public * com.workorder..controller..*.*(..))")
public void logPointCut()
{}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
log.info("=========================================");
long beginTime = System.currentTimeMillis();
//執(zhí)行方法
Object result = point.proceed();
//執(zhí)行時長(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveSysLog(point, time);
return result;
}
private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
// 接收到請求,記錄請求內(nèi)容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String logStr="";
if (isEnvironment("dev")){
String requestUrl = request.getRequestURL().toString() + "?";
//獲取所有參數(shù)方法
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName = enu.nextElement();
requestUrl+=paraName + "=" + request.getParameter(paraName) + "&";
}
logStr+="\r\n請求地址:"+ requestUrl.substring(0,requestUrl.length()-1);
}
//請求的參數(shù)
Object[] args = joinPoint.getArgs();
String params = JSONUtil.toJsonStr(args);
logStr+="\r\n請求方式:"+ request.getMethod();
logStr+="\r\n請求方法Token:"+ request.getHeader("token");
logStr+="\r\n請求參數(shù):"+ params;
logStr+="\r\nIP:"+ request.getRemoteAddr();
logStr+="\r\n響應(yīng)時間:"+ time+" ms";
log.info(logStr);
/**
* 判斷當(dāng)前是什么環(huán)境
* @param str
* @return
*/
private boolean isEnvironment(String str) {
boolean flag = false;
String[] arr = env.getActiveProfiles();
List<String> list = Arrays.asList(arr);
if (list.contains(str)) {
flag = true;
}
return flag;
}
}四、結(jié)果

到此這篇關(guān)于springboot利用@Aspect實現(xiàn)日志工具類的詳細(xì)代碼的文章就介紹到這了,更多相關(guān)springboot日志工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之插入排序,結(jié)合實例形式分析了java插入排序的概念、分類、原理、實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2017-05-05
java8 filter方法、Predicate接口的使用方式
這篇文章主要介紹了java8 filter方法、Predicate接口的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
詳解SpringBoot+SpringSecurity+jwt整合及初體驗
這篇文章主要介紹了詳解SpringBoot+SpringSecurity+jwt整合及初體驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
SpringBoot中application.yml基本配置解讀
文章主要介紹了Spring Boot項目中`application.properties`和`application.yml`配置文件的使用方法和區(qū)別,包括優(yōu)先級、配置文件所在目錄、端口服務(wù)配置、數(shù)據(jù)庫配置、多profile配置以及靜態(tài)資源路徑的指定2024-12-12
idea神級插件及如何安裝Bito插件【Bito-ChatGPT】
這篇文章主要介紹了介紹一款idea神級插件【Bito-ChatGPT】,Bito插件的強大之處在于它可以幫助開發(fā)人員更快地提交代碼,同時還提供了一些有用的功能,如自動補全提交信息、快速查看歷史記錄等,需要的朋友可以參考下2023-04-04

