项目作者: sweat123

项目描述 :
trace implement with aspectj ctw
高级语言: AspectJ
项目地址: git://github.com/sweat123/trace-aspectj.git
创建时间: 2019-01-11T13:04:30Z
项目社区:https://github.com/sweat123/trace-aspectj

开源协议:Apache License 2.0

下载


Trace Aspectj

Usage

Add maven to your project pom.xml

  1. <dependency>
  2. <groupId>com.laomei.trace</groupId>
  3. <artifactId>trace-aspectj</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>

Add aspectj maven plugin

  1. <plugin>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>aspectj-maven-plugin</artifactId>
  4. <version>1.7</version>
  5. <configuration>
  6. <complianceLevel>1.8</complianceLevel>
  7. <source>1.8</source>
  8. <target>1.8</target>
  9. <aspectLibraries>
  10. <aspectLibrary>
  11. <groupId>com.laomei.trace</groupId>
  12. <artifactId>trace-aspectj</artifactId>
  13. </aspectLibrary>
  14. </aspectLibraries>
  15. </configuration>
  16. <executions>
  17. <execution>
  18. <phase>compile</phase>
  19. <goals>
  20. <goal>compile</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>

Add @Trace to the method which you want to trace;

Result

You can get information from log file;

Example

Java class

  1. public class Main {
  2. public static void main(String[] args) throws InterruptedException {
  3. Main main = new Main();
  4. main.hello(1);
  5. main.test();
  6. }
  7. @Trace
  8. public void test() {
  9. System.out.println("test");
  10. }
  11. @Trace
  12. public void hello(int i) throws InterruptedException {
  13. System.out.println("hello");
  14. hi1();
  15. hi2();
  16. hi3();
  17. TimeUnit.SECONDS.sleep(1);
  18. }
  19. @Trace
  20. public void hi1() throws InterruptedException {
  21. System.out.println("hi");
  22. TimeUnit.SECONDS.sleep(1);
  23. }
  24. @Trace
  25. public void hi2() throws InterruptedException {
  26. System.out.println("hi");
  27. TimeUnit.SECONDS.sleep(1);
  28. hi3();
  29. }
  30. @Trace
  31. public void hi3() {
  32. System.out.println("sleep");
  33. }
  34. }

Console log

  1. hello
  2. hi
  3. 20:48:10.225 [main] INFO com.laomei.trace.TraceQueue - prefix-1-2|com.laomei.test.aspectj.trace.Main|hi1|1002ms
  4. hi
  5. 20:48:11.258 [main] INFO com.laomei.trace.TraceQueue - prefix-1-3-4|com.laomei.test.aspectj.trace.Main|hi3|30ms
  6. 20:48:11.258 [main] INFO com.laomei.trace.TraceQueue - prefix-1-3|com.laomei.test.aspectj.trace.Main|hi2|1031ms
  7. 20:48:11.258 [main] INFO com.laomei.trace.TraceQueue - prefix-1-5|com.laomei.test.aspectj.trace.Main|hi3|0ms
  8. sleep
  9. sleep
  10. 20:48:12.258 [main] INFO com.laomei.trace.TraceQueue - prefix-1|com.laomei.test.aspectj.trace.Main|hello|3435ms
  11. test
  12. 20:48:12.258 [main] INFO com.laomei.trace.TraceQueue - prefix-6|com.laomei.test.aspectj.trace.Main|test|0ms

The format of the trace message:

id|className|methodName|time cost

  • hello() is the first method will be traced; The id of this method is prefix-1
  • hi1() is the sub method in hello(); This is the second method which is traced; Id is
    prefix-1-2
  • hi2() is third method which is traced; Id is prefix-1-3
  • hi3() will be accessed in hi2(); Id is prefix-1-3-4
  • test() is the last method will be traced; Id is prefix-6
  1. From these data, you can know that the order of the methods is not in sequential;
  2. But method which is called later will have a more large id; Like h1() and h2()
  3. Methods which are in the same level (hello() and test()), the format of the id will be same; Method which is called later will have a large id;
  4. Although the message is not friendly, but the have topology sort; We can know the order of method and how long each method cost;