The logging module is part of the Python standard library. It is described in PEP 0282.r You can get a first introduction to the module in the Basic Logging Tutorial.

Logging usually serves two different purposes:

  • Diagnosis:

    • You can display the context of certain events.

    • Tools like Sentry group related events and facilitate user identification, etc., so that developers can find the cause of the error more quickly.

  • Monitoring:

    • The logging records events for user-defined heuristics, for example for business analyses. These records can be used for reports or optimisation of the business goals and, if necessary, visualised.

What are the advantages of logging over print?

  • The log file contains all available diagnostic information such as file name, path, function and line number.

  • All events are automatically available via the root logger unless they are explicitly filtered out.

  • Logging can be muted using either of the following two methods: logging.Logger.setLevel() or logging.disabled.

See also

  • loguru, which makes logging almost as easy as using print instructions.

  • structlog adds structure to your log entries.