6

Is there any way that I can log / record / monitor / view every execution of a method inside of a Java program.

For example:

12:30:12 someMethod("argument")
12:30:12 anotherMethos(1, 2, 3)
12:30:13 finalMethod("invalidInputHere1234")
jSherz
  • 927
  • 4
  • 14
  • 33
  • 1
    Have a look at this article: http://extranet.ipl.be/OOMADB/document/IPL_cours/COO/logging-at-loadingTime.htm – ChristopheD May 12 '12 at 10:11
  • 1
    http://stackoverflow.com/questions/4823557/java-logging-method-calls & http://stackoverflow.com/questions/4653013/is-there-an-aspect-already-written-and-tested-well-for-trace-logging – assylias May 12 '12 at 10:12

5 Answers5

3

Easy answer: modify the method, and insert logging statements.

Somewhat more complex answer, which works even if you can't modify the method: look into aspect-based programming.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • I want to trace hundreds of methods so can't really change every single one of them. Is there not a way of monitoring the execution of a Java program to see the methods being run? I know that I can use NetBeans to track a variable's value - what about which methods have been executed? – jSherz May 12 '12 at 10:12
  • 2
    Take a look at [this article](http://mathewjhall.wordpress.com/2011/03/31/tracing-java-method-execution-with-aspectj/) – Amadan May 12 '12 at 10:14
2

Without modifying the code you could use a debugger or profiler that is able to record any state change, like the Chronon time travelling debugger.

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
2

Try this: https://github.com/taobao/TProfiler

1

You can record your program and then play it with Chronon

isvforall
  • 8,768
  • 6
  • 35
  • 50
0

You can use @Loggable annotation from jcabi-aspects, which wraps all methods you want to debug with a simple logging mechanism:

@Loggable(Loggable.DEBUG)
public String load(URL url) {
  return url.openConnection().getContent();
}

It logs through SLF4J.

yegor256
  • 102,010
  • 123
  • 446
  • 597