tags:

views:

244

answers:

4

Logging can get complicated, quickly. Considering that you have some code, how do you add logging to it? What library(ies) do you use?

What are some good code techniques for getting the most out of your logging statements while having minimal impact on your application?

+3  A: 

Libraries: Log4J and Log4Net (for Java and .NET respectively)

From Log4J site:

Inserting log statements into your code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.

On the other hand, some people argue that log statements pollute source code and decrease legibility. (We believe that the contrary is true). In the Java language where a preprocessor is not available, log statements increase the size of the code and reduce its speed, even when logging is turned off. Given that a reasonably sized application may contain thousands of log statements, speed is of particular importance.

With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.

Logging equips the developer with detailed context for application failures. On the other hand, testing provides quality assurance and confidence in the application. Logging and testing should not be confused. They are complementary. When logging is wisely used, it can prove to be an essential tool.

Lou Franco
+1  A: 

Tool for viewing Log4X logs: ChainSaw

Lou Franco
+2  A: 

There has been quite some discussion on this in other questions. If you a C# man take a look at What is the best logging solution for a c# net 3.5 project or What’s your logging philosophy?

Search for Logging and you will find many more. :)

FryHard
A: 

Some aspects I would like to add about logging practices.

  1. Make it a practice to separate functional logging from development logging. In a production environment, functional logging may involve a DBMS or some other resource. We can achieve this by writing a wrappers on actual logger.
  2. In a multi-user environment, log statements become difficult to read, have unique user session-id and log it during development phase. Then a simple perl script can filter out the sections, making debugging simpler. Easy to do using wrapper classes as described above
  3. Wrapper classes also free you from being tied to one specific logging api.
  4. Try aspect oriented approach to logging where possible, this is essentially done using point cuts (method entry/exit points can be easily covered). Cleaning up code will be easier.
questzen