1

I am using the logging module in python project. and I am trying to understand when I should use each of the level? As I understand logging.info(<msg>) is used when the right path of the program is happens.

But when should I use the logging.debug(<msg>) and when I should use the logging.error(<msg>)?

Shaked Eyal
  • 123
  • 1
  • 14

2 Answers2

4

Log levels are mostly convention. A higher log level means the message is more urgent.

Generally, these are the considerations for the standard levels

DEBUG: This log level usually isn't recorded, but it's useful when a developer is trying to reproduce an issue. Feel free to use it liberally, but not so much that you clutter your code with it.

INFO: "This thing happened". This is good for normal operations. Good for answering questions like: "Hey, did the cron job run today?"

WARNING: Something went wrong, but it's not critical. Some metadata was malformed and couldn't be saved for example, but you're still able to do the thing that makes you money.

ERROR: Something went wrong, it's probably bad, but not bad enough to kill the process. You want lots of detail in these logs because you'll use them to diagnose issues when you start, before turning to debug.

CRITICAL: Something is very wrong. Sound the alarm.

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
munk
  • 12,340
  • 8
  • 51
  • 71
1

Very casually defined: 'Info' is for logging details which are interesting to note, but not especially important for fixing some issue. 'Error' indicates some problem occurred in the code, and while program may have continued, information about the error will been logged. 'Debug' indicates an excessive, verbose output of information is written to the log, which is done mostly to pinpoint an otherwise hard to find issue. In other words, a developer uses debug logging to painfully identify each step the code takes in an effort to find and correct some hard to understand error/issue. More formally, each log level is related to a numeric level (e.g. critical 50, error 40, info 30, debug 10, etc.) with lower level categories representing a "finer-grain" of logging detail.

A clarifying metaphor of these log categories is "altitude levels:" the higher up the less detail is/should be visible, whereas "closer to the ground" (or code) more details should be in relief. In "logging space", critical problems and errors are/should be visible from high up, and non-problems (info) only visible when "flying" close to the ground.

gregory
  • 10,969
  • 2
  • 30
  • 42