Using the @Loggable annotation

With @Loggable you can tell AnnoJ to generate logging method calls at the beggining of some methods. For example if you have the following class definition

@Loggable
public class InterestingClass{
     public void importantMethod(){
          //some code here
     }
}

AnnoJ will modify importantMethod() to look like this:

importantMethod(){
     log("importantMethod called");
     //some code here
}

That is you can log when a method gets called without manually writing the logging code. What’s better AnnoJ will log the actual parameter values. Combined with the @ToString annotation this can be a powerful tool.

As with @ToString if you place @Loggable before a class definition then all methods will be logged. If you want to exclude some methods use the @Exclude annotation (just put it before the method you want to leave out). Alternatively you can specify in a String array which methods to include:

@Loggable(methods={"importantMethod"})
public class InterestingClass{
     public void importantMethod(){
          //other methods
     }
}

In this case only importantMethod() will be logged.

If you want that all methods of all classes of a package be logged just define a public class called PackageInfo and annotate it with @Loggable (you cant use parameters here).

The @Loggable annotation uses Log4j so you must provide a properly configured log4j.properties file in the root of your application.

As with the other annotations to use @Loggable you must bootstrap your application.

Advertisement

One Response to Using the @Loggable annotation

  1. [...] is asked to load a class it checks if its methods/fields or the entire class is annotated with @Loggable, @ToString or @Exclude. After that it tries to transform the bytecode of the class according to the [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.