This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Questions for using log4j

 
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?
 
author & internet detective
Posts: 39530
776
Eclipse IDE VI Editor Java
  • 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.
 
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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?
 
Bartender
Posts: 9584
13
Mac OS X Linux 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: 9584
13
Mac OS X Linux 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.
 
This is my favorite show. And this is my favorite tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!