Understanding Log Levels Using A Practical Example

Example: Say you have an authentication API, that receives a JSON request to register a new user.

How would you log events that occur in the end-to-end new user registration process? Here’s how I would do it:

Image for post
Image for post
  • The arrows represent the setting on your logger instance
  • For example, if you set the level to DEBUG, you will see all the logs in your logs.
  • If you set it to warn, you will only see warning, error and fatal logs. Assuming you are using a single logging instance(i.e. All logs go to a single log file)
  • If you use multiple logging instances, you can set each logger to have different levels.
  • That is, you can group all the fatal errors in one file, and set alerts according to your specific needs.
  • FATAL — “Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention” — LogLevel Enum
  • ERROR — “Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.” — LogLevel Enum
  • WARN — “Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.” — LogLevel Enum
  • INFO — Logs that track the general flow of the application.
  • DEBUG —”Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.” — LogLevel Enum

Reference

  • Happy to know what you think in the comments. Thanks for reading.

Written by

Former Glorified Electrician(aka Electrical Engineer). Now a Software Developer working on complex Enterprise Software. Lets connect on twitter @NdamuleloNemakh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store