7 Good Rules to Log Exceptions

I’ve been helping to debug some nasty problems and bugs lately. It occurred to me that some best practices on how to log exceptions go a long way towards easier debugging. Some of the best practices I’ve learned to log exceptions are compiled in this post.

1. Only log technical exceptions not user exceptions
User exceptions are either ok and need not to be logged (“login name already exists”) but shown to the user, or no exception at all (“user has no credit left”). Technical exceptions are those you need to debug (“no file storage left”, “could not book product”) and react to. If you log everything you will probably get too many log entries to have a meaningful reaction to exceptions in your log. You should inquire into every exception in your log files and find the cause for it (“is it a bug?”). Too many exceptions will make you sloppy with exceptions in your log files (“nah, just another exception”).

2. Store data in your exceptions to make them easier to log
Taking the exception “could not charge money from account” you should store the context of the exception just like Junit does (“expected but got …”) to make debugging easier

The message could be: “Tried to charge 20 EUR from account 1234567890 but 10 EUR available” compared to “Charge failed”. This makes it much easier later to log the exception in a meaningful way. Be careful to create no memory leaks though.

3. Log the description of your exception
Very bad example from Sun: The ClassCastException didn’t show you what class you did want an object to cast to for a long time.

Now it even detects

and tells you

During runtime the exception thrown by Java is now:

Much better than before.

4. Output all causes to your exception
If your exception has an exception wrapped as a cause, log all causes. Some logging frameworks do this for you, some don’t. Be sure to have all causes of your exception in the log file. Be sure the beginning of all relevant stack traces in your log, not scrambled ones.

5. Log to the right level
If your Exception is critical, log it as Level.CRITICAL. You need to decide what critical means for you (most often it means losing money). For example if a booking didn’t work, or a user could not register due to technical problems then you have a CRITICAL problem you need to solve.

Monitor your log files for critical exceptions. You’re losing money.

Have your own exception implement isCritical() or a CriticalException interface and test when logging the exception in your wrapper to log it on the right level. If your logging framework hasn’t got an appropriate level, create one.

6. Don’t log and rethrow
Logging and rethrowing an exception is an exception anti-pattern.

Don’t do it. Your log files will then contain the same exceptions several times on several stack levels. Only log the exception once.

7. Do not log with System.out or System.err
Always use a log framework, it can handle logging better than you.

I hope those rule help you with your exception logging and enable you to easier debug your problems.

Thanks for listening. As ever, please do share your thoughts and additional tips in the comments below, or on your own blog (I have trackbacks enabled).

Update: For some Hackernews comments: Logging everything is fine but leads to ~50gig/day, 300gig/week, 1.2tb/month log files for a moderate site. Your grep won’t work very good for that. Splunk will obviously help of course but is only free for 0.5gig/day, a 1/100 of the log files you will get.

191 thoughts on “7 Good Rules to Log Exceptions”

  1. Pingback: best
  2. Pingback: fucoxanthin
  3. Pingback: geniux pills
  4. Pingback: intellux
  5. Pingback: probrain
  6. Pingback: radian c
  7. Pingback: red rhino
  8. Pingback: supreme antler
  9. Pingback: genius reviews
  10. Pingback: testomax
  11. Pingback: TRACKR BRAVO
  12. Pingback: alpha f1
  13. Pingback: alpha fuel
  14. Pingback: alpha man pro
  15. Pingback: alpha peak
  16. Pingback: alpha xtrm
  17. Pingback: caralluma extract
  18. Pingback: cognifocus
  19. Pingback: nutra forskolin
  20. Pingback: premium cleanse
  21. Pingback: pro forskolin
  22. Pingback: forskolin slim
  23. Pingback: garcinia cleanse
  24. Pingback: garcinia xt
  25. Pingback: vitapulse
  26. Pingback: Vita pulse
  27. Pingback: vitapulse reviews
  28. Pingback: vitapulse review
  29. Pingback: pure colon detox
  30. Pingback: somnapure
  31. Pingback: reviews vitapulse
  32. Pingback: vitapulse vitamins
  33. Pingback: vitapulse
  34. Pingback: vitapulse
  35. Pingback: vita pulse
  36. Pingback: taurus traits
  37. Pingback: Diabetes Free pdf
  38. Pingback: Panic Away
  39. Pingback: Maxtropin Reviews
  40. Pingback: Tai Cheng
  41. Pingback: MegaDrox Reviews
  42. Pingback: Test X180 Review
  43. Pingback: Test X180
  44. Pingback: Breast Actives
  45. Pingback: vita pulse
  46. Pingback: probiotic america
  47. Pingback: probiotic america
  48. Pingback: niagen reviews
  49. Pingback: drones for sale
  50. Pingback: Check This Out
  51. Pingback: browse this site
  52. Pingback: news
  53. Pingback: click here now
  54. Pingback: check here
  55. Pingback: Source
  56. Pingback: find more info
  57. Pingback: imp source
  58. Pingback: this website
  59. Pingback: have a peek here
  60. Pingback: have a peek here
  61. Pingback: More hints
  62. Pingback: anchor
  63. Pingback: my site
  64. Pingback: more info here
  65. Pingback: click
  66. Pingback: Read More Here
  67. Pingback: Wakeup Lean Review
  68. Pingback: check my site
  69. Pingback: find more
  70. Pingback: visit
  71. Pingback: their explanation
  72. Pingback: see this
  73. Pingback: official site
  74. Pingback: Visit Website
  75. Pingback: check it out
  76. Pingback: Metabolic Cooking
  77. Pingback: Diy Magic Machine
  78. Pingback: click
  79. Pingback: 60 day fix review
  80. Pingback: click here
  81. Pingback: tires
  82. Pingback: rims
  83. Pingback: wheels
  84. Pingback: read more
  85. Pingback: click here
  86. Pingback: read more

Comments are closed.