1. 程式人生 > 實用技巧 >不通過開發工具來檢視程式的執行日誌

不通過開發工具來檢視程式的執行日誌

首先可以將列印日誌的程式碼放到一個包中,然後稍作修改

這個Aspect的java程式碼如下:

 1 package com.mvc.controller;
 2 
 3 import org.apache.log4j.Logger;
 4 import org.aspectj.lang.JoinPoint;
 5 import org.aspectj.lang.ProceedingJoinPoint;
 6 import org.aspectj.lang.annotation.After;
 7 import org.aspectj.lang.annotation.AfterReturning;
8 import org.aspectj.lang.annotation.AfterThrowing; 9 import org.aspectj.lang.annotation.Around; 10 import org.aspectj.lang.annotation.Aspect; 11 import org.aspectj.lang.annotation.Before; 12 import org.aspectj.lang.annotation.Pointcut; 13 import org.springframework.stereotype.Component; 14 15 @Component
16 @Aspect 17 public class LogAspect { 18 19 Logger log = Logger.getLogger(LogAspect.class); 20 21 @Pointcut("execution(* com.mvc.service.LwlServiceTmpl.get*(..))") 22 public void aop() { } 23 24 // 此方法將用作前置通知 25 @Before(value = "aop()") 26 public void BeforeAdvice(JoinPoint joinpoint) {
27 String classAndMethod = joinpoint.getTarget().getClass().getName() + "類的" + joinpoint.getSignature().getName(); 28 log.info("前置通知:" + classAndMethod + "方法開始執行!"); 29 } 30 31 // 此方法將用作後置通知 32 @AfterReturning(value = "aop()") 33 public void AfterReturningAdvice(JoinPoint joinpoint) { 34 String classAndMethod = joinpoint.getTarget().getClass().getName() + "類的" + joinpoint.getSignature().getName(); 35 log.info("後置通知:" + classAndMethod + "方法執行正常結束!"); 36 } 37 38 // 此方法將用作異常通知 39 @AfterThrowing(value = "aop()", throwing = "e") 40 public void AfterThrowingAdvice(JoinPoint joinpoint, Throwable e) { 41 String classAndMethod = joinpoint.getTarget().getClass().getName() + "類的" + joinpoint.getSignature().getName(); 42 log.info("異常通知:" + classAndMethod + "方法丟擲異常:" + e.getMessage()); 43 } 44 45 // 此方法將用作最終通知 46 @After("aop()") 47 public void AfterAdvice(JoinPoint joinpoint) { 48 String classAndMethod = joinpoint.getTarget().getClass().getName() + "類的" + joinpoint.getSignature().getName(); 49 log.info("最終通知:" + classAndMethod + "方法執行結束!"); 50 } 51 52 // 此方法將用作環繞通知 53 @Around("aop()") 54 public Object AroundAdvice(ProceedingJoinPoint pjp) throws Throwable { 55 long begintime = System.currentTimeMillis();// 記下開始時間 56 // 傳遞給連線點物件進行接力處理 57 Object result = pjp.proceed(); 58 long endtime = System.currentTimeMillis();// 記下結束時間 59 String classAndMethod = pjp.getTarget().getClass().getName() + "類的" + pjp.getSignature().getName(); 60 log.info("環繞通知:" + classAndMethod + "方法執行結束,耗時" + (endtime - begintime) + "毫秒!"); 61 return result; 62 } 63 }

然後編譯執行一下,然後首先檢視專案部署的位置:

找到這個檔案,然後把它複製到tomcat的webapps下,如下圖:

後面直接通過瀏覽器就可以直接訪問了:

然後Tomcat這裡也顯示日誌資訊,可以用來檢視程式執行狀況