File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes Questions for using log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Questions for using log4j" Watch "Questions for using log4j" New topic
Author

Questions for using log4j

Peter Hsu
Ranch Hand

Joined: Aug 25, 2006
Posts: 72
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
Peter Hsu
Ranch Hand

Joined: Aug 25, 2006
Posts: 72


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

Joined: Aug 25, 2006
Posts: 72
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

Joined: Oct 29, 2001
Posts: 8843
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8843
    
    7

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions for using log4j
 
Similar Threads
Unable to set classpath for log4j
log4j category
Can logger instance be static in multithreaded app?
Logger(log4j) in Servlet
How to use log across objects