What are some patterns and anti-patterns of Application Logging?

A few points for Application Logging :

  • Keep all logging code in your production code. Have an ability to enable more/less detailed logging in production, preferably per subsystem and without restarting your program.
  • Make logs easy to parse by grep and by eye. Stick to several common fields at the beginning of each line. Identify time, severity, and subsystem in every line. Clearly formulate the message. Make every log message easy to map to its source code line.
  • If an error happens, try to collect and log as much information as possible. It may take long but it’s OK because normal processing has failed anyway. Not having to wait when the same condition happens in production with a debugger attached is priceless.

Logs are mostly needed for monitoring and troubleshooting. Put yourself in a troubleshooter’s shoes and think what kind of logs you’d like to have when something wrong is happening or has happened in the dead of night.

Logging Antipatterns:

  1. Logging to stderr or stdout( don’t use System.out.println and ex.printStacktrace() )
  2. Complex Logging Statements (not able to understand the context of logging statement)
  3. Wrong Log Level (if you are getting something wrong use error level, not warning)
  4. No Logging at All

References: https://softwareengineering.stackexchange.com/questions/112402/what-are-some-patterns-and-anti-patterns-of-application-logging