Friday, August 19, 2011

A simple guide to effective logging

One of my coding pet peeves is lack of useful log messages. While logging is the simplest, yet effective tool in a developer's arsenal, it is often the most neglected one. We developers do not take the time to think through what and where we should add log messages in our codes.

We tend to use only "error" and "debug" level log messages. We use "debug" messages to help test our codes during development stage. But we add these debug messages on an ad hoc basis. And, our applications usually do not have debug level turned on in production (and we don't want to). The only other time we log messages is when there is an error or exception. This is partly because traditionally developers are not required to provide operational support for the software they write. They are usually removed from production support by 1 or 2 levels- a support team in the client's organization and then another in their own organization. With SaaS model, this separation is going away to some extent, but the issue persists. Well thought out log messages can save a lot of time & frustration with debuging production issues.

Here is a set of rules I use when I write codes,
  1. Use a good mix of 4 levels of log messages- error, warn, info, and debug.
  2. Usually "info" to "debug" ratio is 30%.
  3. "Info" is the most important log level from operations perspective, yet the most neglected and misunderstood one. Systems should run at info level. I see it as the "system's heart beat." At any point in time, anyone should be able to say how the system is doing by reviewing info messages. The trick is to achieve that using the minimum number of messages with just enough useful information.
  4. Use "info" message to trace high level application flow.
  5. Use "debug" message to trace detailed level application flow. Never use "system.out" messages.
  6. Use "warn" level messages when system can recover from a failure. Also use warn when a user is doing anything unusual, i.e. multiple attempt to login, or trying to access functions that they are not supposed to etc. This is specifically useful in the SaaS systems. "Warn" messages can serve as an early warning signal.
  7. Send email when an "error" message is logged. Do not use "error" level if you do not want to receive an email about it.
  8. Be vigilant about what data you write out in a log message. Only enough data to help track activities, but never any security sensitive data (in the context of the domain).
  9. Add context to the log messages. For example, date & time, line#, class name, method name, session id, user id etc. Logging frameworks available in most languages now-a-days are designed after "log4j," one of the most well-designed framework by Ceki Gülcü. Log4j and similar frameworks make it easy to automatically add this context to all messages. 
  10. Format log messages in a way so that it is easy to plop them into a tool like Excel for easier analysis. Here is an example of a good formatted log message,
[ DEBUG] | 110817 21:28:58 | pid-609 | user 2 | session 12345 | controller | effective_logging.rb:17:in `descope_multiple' | wi 12 descoped
Here is an example code showing a good mix of log messages used:



What guideline do you follow for logging?

35 comments:

  1. One of the most required and popular weather measuring instrument is instrument to measure wind speed,wind meters.For measuring barometric pressure, temperature, wind direction, wind speed,humidity;it's crucial.

    ReplyDelete
  2. This includes an extraordinary level of wellbeing to the note. Income from other salary properties likewise can substitute for income from the property being set as security. website link

    ReplyDelete
  3. This combination helps to prepare graduates to transfer relevant, useful skills into the workforce.forbrukeretaten

    ReplyDelete
  4. I might want to thank you for the endeavors you have made in composing this article. I am trusting the same best work from you later on too.. pisaki 3d

    ReplyDelete
  5. Adobe Acrobat a group of projects intended to see, make, control and oversee documents in PDF. Adobe Acrobat is costly yet you can download a free trial variant which you can use for a timeframe.convert pdf to powerpoint

    ReplyDelete
  6. All the blog writings are exquisite. The more I read, the more fall under the spell of them.
    criar loja virtual magento

    ReplyDelete
  7. The log home has a long history. Vitruvius Polio was the primary engineer who depicted development with sign in his Tactato del'architectura (Treatise on design).chicago electric chainsaw

    ReplyDelete
  8. Reduced probability of forced liquidation of the business due to sudden death or permanent disability of business ownerOrder Track

    ReplyDelete
  9. The best business analyst tools enable you to shorten the duration of projects. For example, you can use them to reduce the period between starting a project and having it approved. drive traffic to website

    ReplyDelete
  10. Nonetheless, in spite of the fact that you can consolidate PDF archives with PDF creator, regardless you meander if there is any way empowers you to join them with negligible exertion as you have a large number of PDF documents to go. https://altomergepdf.com/faq

    ReplyDelete
  11. I truly awed after read this in light of some quality work and educational contemplations . I just wanna express profound gratitude for the essayist and want you to enjoy all that life has to offer for coming!. Anti piracy protection

    ReplyDelete
  12. Technology makes subjects open to all understudies, incorporating those with unique needs. diebesten vpn

    ReplyDelete
  13. You need to perform quality control on your telemarketing group and this product enables you to do only that. https://callgear.com/

    ReplyDelete
  14. Give us initial a chance to condense the articles that we are alluding to. top reason to know why your business needs customized software

    ReplyDelete
  15. As a data controller you can likewise be arraigned for offenses, for example,
    pengeluaran hk

    ReplyDelete
  16. The technology comprises drone detection radar and electronic warfare systems, a visual target recognition camera and a 3-D user display to provide the warfighter with advanced situational awareness. Anti Drone System

    ReplyDelete
  17. DGS’s military grade Drone Master™ Counter Drone System detects, tracks, identifies and defeats threats posed by hostile UAS. Drone Jammer

    ReplyDelete
  18. As security work force landed nearby, the boss would content a catchphrase and an ID number. Bulk SMS API

    ReplyDelete
  19. To assess technology before settling on any speculation choice, firms need to know the pace of licensing action in the technology, which licenses encapsulate major thoughts in the technology and how helpless the company's advancements are to patent encroachments. www.topcatbreeds.com

    ReplyDelete
  20. This is such a great post, and was thinking much the same myself. Another great update. 審査の甘い消費者金融

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Comprehend that it's the means by which we carry on with our lives. So don't simply push it out.Access Control System

    ReplyDelete
  23. Please continue this great work and I look forward to more of your awesome blog posts. what is an aae file, what are aae files

    ReplyDelete
  24. Hi there! Nice post! Please tell us when I will see a follow up! körkortsprov

    ReplyDelete
  25. What materials would you be able to use in your business? HispeedLaser

    ReplyDelete
  26. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks PCB assembly

    ReplyDelete
  27. Manage Engine Service Desk Plus is a Web-based, simple to utilize HelpDesk programming with Asset Management whose highlights incorporate agreement the executives, programming permit the board, network stock administration, buying, dynamic index and LDAP combinations and information the board functionalities.help desk softwares

    ReplyDelete
  28. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... names for organization

    ReplyDelete
  29. MURAHQQ adalah situs permainan kartu Online terlengkap aman dan terpercaya dengan server kualitas terbaik dan tercepat tanpa admin ataupun robot yang ikut bermain serta bonus kemenangan nyata ratusan juta

    ReplyDelete
  30. This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. unique business names

    ReplyDelete
  31. I’ve been searching for some decent stuff on the subject and haven't had any luck up until this point, You just got a new biggest fan!.. unique company name

    ReplyDelete
  32. I wish more authors of this type of content would take the time you did to research and write so well. I am very impressed with your vision and insight. tech company names

    ReplyDelete
  33. I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work. unique business names

    ReplyDelete
  34. 80w co2 laser belongs to lasers with low power.

    ReplyDelete