Automatic generation of entrance and exit logging

C

Chishun Kwong

Hello,

I have a need to add entrance and exit logging to all the public
methods of all classes of a certain package. While I don't have that
many of them and putting the logging in manually is an option, I am
wondering if there is a better way.

I am thinking of using the doclet api to discover what a class is
consist of and then "reconstruct" the source file, adding the logging
along the way, and then recompile it. For example, after learning that
I have a method that looks like this

public int getACertainNumber ()
{
< method body >
}

I will recreate the method, but add the logging, like this

public int getACertainNumber ()
{
Logger.getInstance.log("Entered getACertainNumber");
try {
< method body >
} finally {
Logger.getInstance.log("Exited getACertainNumber");
}
}

I know in advance that all my classes are simple enough that I don't
have to worry about special things like static initializers and inner
classes, however, even with this, I find this impossible to do with
the doclet api because it does not give me the method body.

Does anyone know a tool comparable to javadoc/doclet that will give me
the method body in addition to the class's metadata? Alternatively,
any suggestion to tackle this problem? I am sure someone must have
done this before.

I really think this is a better way to approach this problem because
it is much easier to maintain, and I can turn off the logging
altogether (they are for performance analysis only).

Thanks for any suggestion,
Chishun Kwong.
 
C

Christophe Vanfleteren

Chishun said:
Hello,

I have a need to add entrance and exit logging to all the public
methods of all classes of a certain package. While I don't have that
many of them and putting the logging in manually is an option, I am
wondering if there is a better way.

I am thinking of using the doclet api to discover what a class is
consist of and then "reconstruct" the source file, adding the logging
along the way, and then recompile it. For example, after learning that
I have a method that looks like this

public int getACertainNumber ()
{
< method body >
}

I will recreate the method, but add the logging, like this

public int getACertainNumber ()
{
Logger.getInstance.log("Entered getACertainNumber");
try {
< method body >
} finally {
Logger.getInstance.log("Exited getACertainNumber");
}
}

I know in advance that all my classes are simple enough that I don't
have to worry about special things like static initializers and inner
classes, however, even with this, I find this impossible to do with
the doclet api because it does not give me the method body.

Does anyone know a tool comparable to javadoc/doclet that will give me
the method body in addition to the class's metadata? Alternatively,
any suggestion to tackle this problem? I am sure someone must have
done this before.

I really think this is a better way to approach this problem because
it is much easier to maintain, and I can turn off the logging
altogether (they are for performance analysis only).

Thanks for any suggestion,
Chishun Kwong.

checkout http://just4log.sourceforge.net/
It can add the entry/exit method log calls in the compiled classes.
 
E

Eduardo Francos

Chishun Kwong wrote on 09/15/2003 10:14 PM:
Hello,

I have a need to add entrance and exit logging to all the public
methods of all classes of a certain package. While I don't have that
many of them and putting the logging in manually is an option, I am
wondering if there is a better way.

I am thinking of using the doclet api to discover what a class is
consist of and then "reconstruct" the source file, adding the logging
along the way, and then recompile it. For example, after learning that
I have a method that looks like this

public int getACertainNumber ()
{
< method body >
}

I will recreate the method, but add the logging, like this

public int getACertainNumber ()
{
Logger.getInstance.log("Entered getACertainNumber");
try {
< method body >
} finally {
Logger.getInstance.log("Exited getACertainNumber");
}
}

I know in advance that all my classes are simple enough that I don't
have to worry about special things like static initializers and inner
classes, however, even with this, I find this impossible to do with
the doclet api because it does not give me the method body.

Does anyone know a tool comparable to javadoc/doclet that will give me
the method body in addition to the class's metadata? Alternatively,
any suggestion to tackle this problem? I am sure someone must have
done this before.

I really think this is a better way to approach this problem because
it is much easier to maintain, and I can turn off the logging
altogether (they are for performance analysis only).

Thanks for any suggestion,
Chishun Kwong.

Take a look at Aspect Oriented Programming AOP. I use AspectJ
(http://eclipse.org/aspectj/).
It can do much more, but one of its most common uses is exactly to solve
the problem you describe.

Eduardo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top