Having your application create log files can be the difference between effective and nonexistent remote support ability. As IDE's become more prevalent in development shops it would seem that the art of logging has started to decline with developers. Good logging in an application takes time and can be the best debugging tool you have. Conversely, poor logging can lead a developer down the wrong path wasting time, energy, and money. With this in mind I present to you my top 5 logging tips
1) Use A Framework
There are many open source logging frameworks that can make your life much easier. As developers we like to have control over the nitty gritty details, but reinventing the wheel to implement logging is a waste of valuable cycles when there are so many good free logging packages out there. My personal favorite is the Log4j framework. While this framework was made by the Apache foundation for the java language it has been ported into many different languages.
2) Practice Practice Practice!!!
How many times have we all heard that growing up. When it comes to logging practicing does help developers by helping them form a good habit. If you get used to logging, even in your smallest scripts and programs then when it comes time for the big project you will not even think about where and when to log, it will be done by instinct.
3) Read Your Own Logs
This drives me crazy!! How many times have you been handed a huge log file only to start reading it and you realize that it is complete gibberish. When you are developing the code, you know what to expect and you tend to shortcut your logging strings. If you are going to take the time to log a message make sure you are saying something that will make sense. Before you promote or release your code, hand someone some of your log file and ask them to read it. If they can get the jist of what is happening then you are in good shape. If they look at you like you have 3 heads then you might want to look it over.
4) Develop A Standard
A coding style is unique to a developer. With any language there are multiple ways to do the same things. This is also true in logging. Take the time to developer your own logging standard and try to stick to it. The easier your logs are to read the faster you can get to the root of the problem. Also the more standardized you make the message the easier it will be for your co-workers to help debug your program. This can be the difference between a 3am phone call and a peaceful night of sleep.
5) Don't Be Sloppy
Take 5 min during your development time to make sure you logging is clean, concise and presentable. Over the course of your development and the thousands of lines of code you are writing there is no way you are going to remember what your vague log statement meant. Or worse your boss gets a log file from the latest build and you forgot to take out that late night comment you put in about his hair peice. No matter what the situation is when you release that log into the wild you have no idea who will be reading it, so make sure you mean what you say.
Lastly, I wanted to write a quick tip for those of us working in loosely typed languages like perl and ruby. Don't always assume your string is a string. Most languages have a way of finding out what type or class a variable is. Get in the habit of adding this information to your log statements as well. I tend to add this information surrounded by parentheses in my log statements. This information will save you major headache and many wasted hours when you start seeing strange behavior because that argument you swore was a string happens to be an int.