File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes best places to put log inside source code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "best places to put log inside source code?" Watch "best places to put log inside source code?" New topic

best places to put log inside source code?

Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi, I need some opinion about which are the best places where we should log in a source file..! Currently I am putting a log at entry, exit of method and inside the catch block.

What are the various concern places where we can log ?

I don't want to log unnecessary as later there may be file system full issue..
also Is it good to log the user private data ?

That private data can help us while debugging but is it good to log that ?

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello Sunny,

Generally, we log something from the development perspective and from the enduser (client) perspective.

At the time of development, you can put logging statements at various places that you have mentioned so that you can track the flow of application and any malfunctioning.

But once you deliver your code to client, you should be careful that you log only those things that are necessary and can give some help to track your bugs.

And in disk space issue,

i use log4j framework where once you change the level you can stop logging the lower level logging statements.So that's not a big deal.and you don't need to remove them from code.

And there is nothing like best places.You can put it wherever required.
[ June 02, 2008: Message edited by: Vishal Pandya ]
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Sunny, you can always have different levels of logging, which you can control by some input parameter to your logger framework. At basic, entry and exit logging seems fine, but yeah, for debugging you might like to see private data, sql statements, responses etc (in case if there are such things in your code)...
Jelle Klap

Joined: Mar 10, 2008
Posts: 1951

You may want to take a look at Aspect Ori´┐Żnted Programming (AOP) and use that approach to add generic logging advice to your classes, which in my opinion is ideally suited for the kind of logging actions you describe (on-enter, on-exit, parameters etc.).
During development the logging advice can be enabled, whereas in production it can be just as easily disabled.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
I agree. Here's the link:
subject: best places to put log inside source code?
It's not a secret anymore!