I want to comment out all calls to an API (java.util.Logging, in my case) in my codebase. Is there a good library to accomplish this easily? I tried Eclipse ASTParser, but that is tied to Eclipse. I am now struggling with PMD's parser. I haven't yet looked at it, but can Jackpot do this? Any other suggestions?
sed? Just substitute with a commented line. You'll need to think about the match regex to catch multiline calls.
I know this isn't what you asked for, but I just want to draw your attention to the fact that you can turn logging OFF in your configuration file.
You can use slf4j's jul-to-slf4j module to redirect all java.util.logging calls into slf4j, and then you can choose the slf4j-nop module to ignore all the logging statements.
Will this do, or do you REALLY need to get rid of these source lines?
using a tool to comment out or remove all of the logging code might be a bad idea.
There might be side effect code that you don't notice:
while ( x > 5)
{
// some code here
Logger.global.finest( "X is now bigger: " + (x++) );
}
thats a real obvious example, but it could be very subtle with a method call or something inside the logger call.
It is probably safer to turn off the logging output, or go through the whole thing manually...
If you're looking to avoid evaluation of arguments, as you say in your comments, then I assume you have calls like:
logger.log("Expensive toString() operation on " + this);
which you want to disable ?
If that's the case, and it's scattered through your code, I'd use something like AspectJ to surround all instances of the above with:
if (loggingEnabled) {
...
}
and set the loggingEnabled
flag to false.
If you wanted to comment out this:
Log.doLog("Here is a" /* With a block comment to make it hard */
+ " multiline log statement"
); doSomethingEssential();
then you'd have a trickier time of it because you'd need to parse the code to some extend to know where to put the comments. I.e. you want to be able to do this:
// Log.doLog("Here is a" /* With a block comment to make it hard */
// + " multiline log statement"
// ); // Line break inserted here
doSomethingEssential();
It's much more trivial to do this instead:
if (false) Log.doLog("Here is a" /* With a block comment to make it hard */
+ " multiline log statement"
);
The 'if false' part ends up being optimised out by the compiler and the Log statement won't make it to the final .class file.
That way all you need is a search/replace step in your build script that replaces all occurences of
"Log.doLog("
with either
"if (false) Log.doLog(" or
"if (true) Log.doLog("
without resorting to tricky parsing of the code.