Logging Attribute Access
Logging Attribute Access and Mutation. Sometimes you need to keep track of what your code does and how your programs flow. One way to do that in Python is to use
00:14 This module provides all the functionality that you would require for logging your code. It allows you to constantly watch the code and generate useful information about how it works.
If you ever need to keep track of how and when you access and mutate a given attribute, then you can take advantage of
property() for that, too.
Here, you first import
logging and define a basic configuration. Then you implement
Circle with a managed attribute
The getter method generates log information every time you access
.radius in your code.
The setter method logs each mutation that you perform on
.radius and also logs those situations in which you get an error because of bad input data.
On-screen, you can see this logged version of
Circle in action.
02:17 Logging useful data from attribute access and mutation can help you debug your code. Logging can also help you identify sources of problematic data input, analyze the performance of your code, spot usage patterns, and more.
02:35 In the next section of the course, you’ll see how to manage the deletion of attributes.
Become a Member to join the conversation.