• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions for using log4j

 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have read quite a bit of code at my work using log4j
I am curious why no body really does this




If I have this global flag, I can have compiler throw away my logger.Debug() method call and improve my performance slightly on release right? Or am I getting it wrong?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
In theory. The thing is the amount of time to call a method is trivial. Creating extra code all over to deal with it isn't worth the maintenance complexity.

Log4j does shield you from the actual logging and processing if the level does not match though.
 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I agree for most situations, but if the expression in the parameter of the log statement is something "expensive" like retrieving an attribute from an entity bean or maybe a larger calculation I think it may make sense to add an if statement, because without it the parameter will be evaluated before the method debug() is called.

But you don't have to declare a constant, because you can use the method isDebugEnabled() from log4j.

for example (I have to admit this is a little theoretical):



What do other people think of my opinion?

John
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do other people think of my opinion?

Yes, if you have something heavy you'd like to output for debugging purpose, it's easier to use isDebugEnabled().
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Correct me if I am wrong here. In order for the compiler to throw away code, shouldn't the if statement refer to a compile time constant?
Method call is not compile time constant right?

My goal is to let compiler throw away all the debug method call to save space. Considering this, above code kind of defeat the purpose right?
 
Peter Hsu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Peter,
In theory. The thing is the amount of time to call a method is trivial. Creating extra code all over to deal with it isn't worth the maintenance complexity.

Log4j does shield you from the actual logging and processing if the level does not match though.


I agree. Java programmers care more about robustness and readability than efficiency. However, in the extreme example when I have debug code everywhere and my program need to run on a mobile device on release, would this be a concern?
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Hsu wrote:
My goal is to let compiler throw away all the debug method call to save space. Considering this, above code kind of defeat the purpose right?


What John Ben proposes is the best practice per the log4j FAQ.
 
Joe Ess
Bartender
Posts: 9318
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One other thing: if you find using logger.isDebugEnabled() tedious, there is some functionality available that were developed for the abandoned log4j 1.3 version that makes building strings on-the-fly efficiently less tedious by using MessageFormat. Rather than this:



one would write:


In my opinion it is easier to read than the isDebugEnabled() alternative.
See LogMF for more on that class and see Apache Extras Companion for more on the package.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic