不通过开发工具来查看程序的执行日志

首先可以将打印日志的代码放到一个包中,然后稍作修改

这个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这里也显示日志信息,可以用来查看程序运行状况

原创文章,转载请说明出处,谢谢合作
原文地址:https://www.cnblogs.com/lwl80/p/13651136.html