When to use logging¶

You can access logging functionality by creating a logger via logger = getLogger(__name__), and then calling the logger’s debug(), info(), warning(), error() and critical() methods. To determine when to use logging, and to see which logger methods to use when, see the table below. It states, for each of a set of common tasks, the best tool to use for that task.

Task you want to performThe best tool for the task
Display console output for ordinary usage of a command line script or programprint()
Report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation)A logger’s info() (or debug() method for very detailed output for diagnostic purposes)
Issue a warning regarding a particular runtime eventwarnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning A logger’s warning() method if there is nothing the client application can do about the situation, but the event should still be noted
Report an error regarding a particular runtime eventRaise an exception
Report suppression of an error without raising an exception (e.g. error handler in a long-running server process)A logger’s error(), exception() or critical() method as appropriate for the specific error and application domain