So is there anything you can add, comment or find questionable with these ways of using the logging? Feel free to answer or comment even if it is not related to Java, Java and log4j is just an implementation of how this is reasoned.

6 Answers
6

As an extension of your rule of logging where in the application the log statement came from, you may want to add per module level logging flags. Instead of logging everything, all the time, this allows you to selectively target sections of your application instead. There is overhead in this, and you need to create a facility that allows you to enable / disable that logging. Ideally, you would be able to enable / disable on-the-fly as the application is running.

I'm used to seeing a layer below debug which I term "Trace", but that's not necessarily a universal term. "Trace" level logging tracks as much as you can possibly stand, including module entry / exit, timestamps with entry / exit, and bonus points for capturing passed values. Obviously, that generates a LOT of data and it's not something you turn on willy-nilly. But it has advantages with respect to debugging when you can't attach to the process or you don't have a core dump of the errant application.

I like to see file / module references and timestamps with my log information. It can be handy when trying to hunt down race conditions between threads as well as coordinating the activities of multiple areas of the application. To be fair, I know of some folk who think these details clutter the log file. Adding in the timestamp is something to discuss with the team. (Apologies if log4j already does that.)

If the logging isn't being taken care of by it's own thread / process, that's something to consider as well. Instead of making the application thread wait for the logging to process, the log message gets passed off to the log handler and the application thread goes on its merry way. Alternatively, creating some sort of buffer mechanism to handle the log messages is another way to speed up application responsiveness.

Having controls on the size and history of log files is another feature to consider. You don't want the app blowing out all the disk space on the host system, nor do you necessarily want to keep all log files for all eternity.

One thing to keep in mind is to check the logging level before you do any kind of string operations for logging. That is, don't go to all the work of setting up a date formatter or concatenating a bunch of strings to create the log message if you aren't actually going to log it. That's just wasted work that slows your application down.

I do not find anything questionable in what you have added here Nick. This is how I have been doing it for some time . The post that you have provided is very detailed and probably can be used as a kind of a tutorial for logging. However, i do want to add one thing here, which is : In many places, i have been seeing chaps using conditional logging, for e.g. :

In addition to logging the Class / Method that raised the error, it would be useful to also log the parameters passed into that method. Knowing where an error was raised isn't very useful if it only happens 1 time out of 1000; you also need to know what data caused the error to occur.

I've also found it useful to have a variable which defines the default level of logging for an application. That way you can have DEBUG and INFO code alongside WARNING and ERROR code. When running in production mode you default it to not output DEBUG info, but when a bug shows up you can update a flag and start writing DEBUG messages to the log as well.

One thing I would suggest would be that you have a means of having multiple logging contexts associated with any particular log file, and arrange so that anything written to any logging context will get logged unless code explicitly asks the logging context to discard its contents. Such a design will allow one to capture rather detailed logs during an operation and discard them if the operation succeeds, but have them available if the operation fails. Absent such a logging feature, having good logs available when something fails may require that an application waste lots of time logging useless data in the 99.99% of times when everything works.

Logging is most of the time a cross cutting concern. Java alone is not sufficient expressive to make you able to seperate the logging from the your actual business logics. This means you cannot for example just take one method and put it into another project but you you have to remove and adjust all your logging before you do. And that is just the tip of the iceberg.

To prevent that and other issues when mingling logging and "real" business logics you should regard using aspect oriented programming. For Java the most used framework would be AspectJ. There is this youtube video from google tech talks that explains AspectJ and its uses beyond logging pretty well. You will find many examples for logging yourself of course here on stackexchange too.