The Logging and Warning Modules

We often need to make internal object state and state transitions more visible. There are the following three common cases for increased visibility:

  • One case is for auditing an application, where we want to keep a history of state changes for an object.
  • Another situation is to track the secure operations of an application and identify who is carrying out sensitive actions.
  • A third common situation is to help debug problems that arise during use.

The Python logger is a flexible way to make internal object states and state transitions visible. 

There are times when we have multiple logs with different kinds of information. We might distribute security, audit, and debugging into separate logs. In other cases, we might want a unified log. The logging module permits a variety of configurations.

Some users may want verbose output to confirm that the program works as they understand it. Allowing them to set a verbosity level produces a variety of log details focused on the needs of users.

The warnings module can also provide helpful information for developers as well as users, including the following:

  • In the case of developers, we may use warnings to show them that an API has been deprecated
  • In the case of users, we might want to show them the results are questionable but not erroneous

There might be questionable assumptions or possibly confusing default values that should be pointed out to users.

Software maintainers will need to selectively enable logging to perform useful debugging. We rarely want blanket debugging output: the resulting log might be unreadably dense. We often need focused debugging to track down a specific problem in a specific class or module. The idea of multiple logs being sent a single handler can be used to enable detailed logging in some places and summary logging in others. The logging package in the Python 3.7.2 version of the standard library doesn't have complete type hints. Consequently, the examples in this chapter won't have type details. In this chapter, we will cover the following topics:

  • Creating a basic log
  • Configuration Gotcha
  • Specialized logging for control, debug, audit, and security
  • Using the warnings module
  • Advanced logging—the last few messages and network destinations
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset