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 perform | The best tool for the task |
---|---|
Display console output for ordinary usage of a command line script or program | print() |
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 event | warnings.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 event | Raise 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 |