5

for example I want to replace before compilation:

#debug("${enclosing_method} this is debug message for " + userName)

with:

if (log.isDebugEnabled())
{
  log.debug("<real method name> this is debug message for " + userName);
}
oshai
  • 14,865
  • 26
  • 84
  • 140
  • I use eclipse and ant. is there a way to achieve that with these tools? – oshai Oct 14 '10 at 22:26
  • @ahadshai: Use AspectJ, as Travis mentions. Logging is a cross-cutting concern. Do not litter your implementation with duplicated code. AspectJ is powerful enough that it can inject the code you want to execute everywhere you want it executed and when you want it executed. – Dave Jarvis Oct 14 '10 at 22:35
  • Whatever the quality of your case for "preprocessing" Java, you're in for a long steep climb pushing a heavy rock. It's generally considered bad form, and your example is also a bad one, as others have noted log4j does this for you. You might consider a code/direct bytecode generation approach if it *really* matters. But I suspect you're trying to solve a problem you don't know if you have yet... – andersoj Oct 14 '10 at 22:36
  • I will fix the question. I know log4j is checking for me if debug is enabled. but sometimes the string is compound of few arguments like "hello" + name, so this way I prevent the sting concatenation – oshai Oct 14 '10 at 22:45
  • Nevertheless -- you should be suspicious if entire frameworks already exist to accomplish what you're asking, and you're trying to invent it from scratch. The question of performance penalty for getting the current method name, concatenating strings, etc... is already dealt with for 99.9% of the cases using logging best-practices, well documented elsewhere. If you *really* have a performance issue here, because you're in the 0.1%, you should measure it to make sure. Otherwise, much faster to use what smart people have already provided. (java.util.logging, log4j, commons logging, etc...) – andersoj Oct 14 '10 at 22:48

2 Answers2

5

Don't. Use slf4j instead which allows you to avoid the check with {}.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2

Consider using log4j if your example reflects your primary aim.

Quote needed: Preprocessor usage is bad OO practice could be of interest as well.

Community
  • 1
  • 1
zellus
  • 9,617
  • 5
  • 39
  • 56
  • the example is using log4j. I didnt supply the full source code. I want the code to be robust to method name change and to be short and elegant :) – oshai Oct 14 '10 at 22:25
  • Sorry for not getting your point at first. Are you looking for an elegant way to determine the class name. http://www.java2s.com/Code/Java/Reflection/Getthecurrentmethodname.htm could be a starting point. – zellus Oct 14 '10 at 22:42
  • It is a start, but not elegant one...:) – oshai Oct 14 '10 at 22:56
  • I agree, it's far from being elegant! – zellus Oct 15 '10 at 05:16
  • apperanly this is going to be possible in java 7: http://weblogs.java.net/blog/forax/archive/2010/10/26/how-get-current-class-java I hope they will do it also for method name – oshai Oct 27 '10 at 17:40