Skip to content

Make Java apps troubleshooting easier: Java8 compiler plugin that adds entry/exit trace logging to the methods in your classes automatically.

License

Notifications You must be signed in to change notification settings

y-petrov/autotrace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

autotrace

The project assists the Java apps troubleshooting via automatic adding entry/exit logging to the methods of the project classes. Technically speaking, the project provides Java8 compiler plugin that automatically adds a trace logging to your code during compilation.

The example below shows how to use it and what value it brings.

Let's say we have test Java class test/test/subpkg/yp/Test01.java. We compile it and then run:

user@host:~/git/autotrace/src/test/resources> javac test/test/subpkg/yp/Test01.java 
user@host:~/git/autotrace/src/test/resources> java test.test.subpkg.yp.Test01
hardcoded - static init 1
hardcoded - instance static init 1 - nested block
hardcoded - instance init block 1
hardcoded - constructor without param
hardcoded - in main
user@host:~/git/autotrace/src/test/resources>

Good, it works as designed. Now we want to see the execution tracing. To do this we need ro recompile the source with autotracer plugin:

user@host:~/git/autotrace/src/test/resources> javac -cp ../../../target/auto-tracer-0.1.jar -Xplugin:AutoTracerPlugin test/test/subpkg/yp/Test01.java
test/test/subpkg/yp/Test01.java:70: warning: No Autotrace for empty block
        {
        ^
1 warning
user@host:~/git/autotrace/src/test/resources>

Autotracer printed one warning - it's OK, the empty blocks { }, both method and class init, are not augmented with entry/exit logging.

Let's run it:

user@host:~/git/autotrace/src/test/resources> java test.test.subpkg.yp.Test01
hardcoded - static init 1
hardcoded - instance static init 1 - nested block
hardcoded - instance init block 1
hardcoded - constructor without param
hardcoded - in main
user@host:~/git/autotrace/src/test/resources>

Oops... No traces... :( It's OK, we're almost there. We need to make last step - we need to tell to Java logging framework that we want see the output of .entering() and .exiting() methods of the logger named test.test.subpkg.yp.Test01. There is the number of ways to do this, we'll go with config file approach. We create the config file my-test-logging.properties in the current directory. Here is its content:

# Let's use only console output
handlers= java.util.logging.ConsoleHandler

# Default logging level
.level= INFO

# Setup console output
java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# Now enable entry/exit for one of the parents of the logger of our test class
test.test.subpkg.level = FINEST

Now we run our code again. This time we tell JVM were logging config must be taken from:

user@host:~/git/autotrace/src/test/resources> java -Djava.util.logging.config.file=./my-test-logging.properties test.test.subpkg.yp.Test01

...and here is the output:

Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <static init>
FINER: ENTRY
hardcoded - static init 1
hardcoded - instance static init 1 - nested block
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <static init>
FINER: RETURN
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 main
FINER: ENTRY
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <instance init>
FINER: ENTRY
hardcoded - instance init block 1
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <instance init>
FINER: RETURN
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <instance init>
FINER: ENTRY
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <instance init>
FINER: RETURN
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <init>
FINER: ENTRY
hardcoded - constructor without param
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 <init>
FINER: RETURN
hardcoded - in main
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 m1
FINER: ENTRY
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 m1
FINER: RETURN
Apr 16, 2018 10:51:34 AM test.test.subpkg.yp.Test01 main
FINER: RETURN
user@host:~/git/autotrace/src/test/resources>

In general - that's it. In my past when I had to find why JavaEE app does something wrong, the enabling tracing helped ne a lot. I hope this plugin will help you too.

One more thing to tell - the plugin has been developed and tested with Java8 as of:

user@host:~/git/autotrace> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (IcedTea 3.6.0) (build 1.8.0_151-b12 suse-10.18.2-x86_64)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
user@host:~/git/autotrace>

About

Make Java apps troubleshooting easier: Java8 compiler plugin that adds entry/exit trace logging to the methods in your classes automatically.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages