Peeer Peeer - 1 year ago 139
Java Question

How can i log every method called in a class automatically with log4j

I have a class with database calls, and I generally want to log every method called (with arguments) in this class with log4j:

logger.debug("foo(id="+id+") initiated");

Is it possible to do this automatically? Maybe by using some sort of annotation in start of each method instead of writing every single logger.debug?

Today I have to update my logging.debug every time I change arguments or method name.

Answer Source

Try @Loggable annotation and an AspectJ aspect from jcabi-aspects (I'm a developer):

public String load(URL url) {
  return url.openConnection().getContent();

All method calls are logged through SLF4J.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download