Let’s get started!
To begin with: what is logging? It’s basically a way to keep track of what your program does and how it behaves. You know, like when you have a friend who keeps a journal or diary (or maybe that’s just me…). But instead of writing down your thoughts and feelings about life, Python logs help you understand the inner workings of your code.
So why should you care? Well, for starters, it can save you time and headaches when debugging. Instead of staring at a screen full of error messages, you can see what’s going on behind the scenes in real-time (or close to it). Plus, logging is super easy to set up!
Here’s an example:
# Import the logging module
import logging
# Configure the logging settings
logging.basicConfig(filename='mylogfile.txt', level=logging.DEBUG)
# Define a function
def my_function():
# Assign values to variables
x = 10
y = 5
# Calculate the result
result = x + y
# Log a debug message
logging.debug('Starting function')
# Log an info message with the values being calculated
logging.info('Calculating {} and {}'.format(x, y))
# Log a debug message with the result
logging.debug('Result is: {}'.format(result))
# Check if the result is greater than 10
if result > 10:
# Log a warning message
logging.warning('The result is greater than 10!')
else:
# Log an error message
logging.error('Something went wrong...')
# Call the function
my_function()
# Output:
# mylogfile.txt
# DEBUG:root:Starting function
# INFO:root:Calculating 10 and 5
# DEBUG:root:Result is: 15
# WARNING:root:The result is greater than 10!
# Explanation:
# The logging module is imported to allow for logging messages to a file.
# The logging settings are configured to set the level of logging to DEBUG, which will log all messages.
# A function named my_function is defined.
# The variables x and y are assigned values.
# The result is calculated by adding x and y.
# A debug message is logged to indicate the start of the function.
# An info message is logged with the values being calculated.
# A debug message is logged with the result.
# An if statement checks if the result is greater than 10.
# If the result is greater than 10, a warning message is logged.
# If the result is not greater than 10, an error message is logged.
# The function is called, which executes the code within it.
# The output shows the messages logged to the file, with the level of each message and the message itself.
# The warning message is logged because the result is greater than 10.
In this example, we’re using the `logging` module to log some information about our function. We first set up a basic configuration by specifying a filename and level (which determines what messages get logged). Then, inside our function, we use logging statements to record different events. The output will look something like this:
# Import the logging module
import logging
# Set up basic configuration for logging
logging.basicConfig(filename='myapp.log', level=logging.DEBUG)
# Define a function to calculate the sum of two numbers
def calculate(x, y):
# Log an informational message about the calculation being performed
logging.info('Calculating {} and {}'.format(x, y))
# Log a debug message indicating the start of the function
logging.debug('Starting function')
# Perform the calculation and store the result in a variable
result = x + y
# Log a debug message indicating the result of the calculation
logging.debug('Result is: {}'.format(result))
# Check if the result is greater than 10
if result > 10:
# Log a warning message if the result is greater than 10
logging.warning('The result is greater than 10!')
# Return the result
return result
# Call the function and store the result in a variable
sum = calculate(10, 5)
# Print the result
print(sum)
# Output:
# 15
Pretty cool, right? You can customize the format of your logs by tweaking the `basicConfig()` function (check out the documentation for more info). And if you want to log messages to a console instead of a file, just replace `filename=’mylogfile.txt’` with `stream=sys.stdout`.