tags:

views:

173

answers:

6

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?

A: 

sed? Just substitute with a commented line. You'll need to think about the match regex to catch multiline calls.

Charlie Martin
Show me a good regex to match multiline calls. :-)
binil
well, it's going to have a ';' in it.
Charlie Martin
There could be a ';' in the logged string. Now you're writing a parser, worrying about escaped quote characters and regretting ever starting the task.
izb
Oh, let's send out for a grip here, shall we? I said it would have a ';' in it. It's also going to have a number of other things in it. Unless your string includes the calling sequence in which it's contained, you'll be okay. Something like /\.info(\".*\")\.*;\.*$/ should be a start.
Charlie Martin
+4  A: 

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.

Bill the Lizard
Thanks Bill, although that functionally achieves the same, that is not what I am after. I wanted to comment out the logger.log(..) calls so that the arguments are not evaluated.
binil
No problem. I'm going to leave this here rather than deleting it, since others may find it useful someday.
Bill the Lizard
A: 

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?

Thorbjørn Ravn Andersen
A: 

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...

John Gardner
If this is in your code, your deserve the side effects ;-)
Robin
A: 

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.

Brian Agnew
It would be nice to know *why* somebody voted this down ?
Brian Agnew
+1  A: 

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.

izb
izb, I ended up using this solution. I used sed to make this change. Thanks for taking the time to write this solution up, instead of making useless, supposedly-humorous-but-lame, mysterious comments which does not add anything to the discussion at hand. :-)
binil