It can help you identify errors and issues in your code that might not be immediately apparent otherwise.In Python, logging comes built-in with a powerful module called…you guessed it `logging`. Time to get going with how to use this beast.
To begin with: import the `logging` module at the top of your file (or script). This will give you access to all its functions and classes. Here’s an example:
# Import the logging module
import logging
# Set the logging level to DEBUG
logging.basicConfig(level=logging.DEBUG)
# Create a logger object
logger = logging.getLogger(__name__)
# Create a file handler and set its level to DEBUG
file_handler = logging.FileHandler('my_log_file.log')
file_handler.setLevel(logging.DEBUG)
# Create a formatter and add it to the file handler
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(file_handler)
# Log a debug message
logger.debug('This is a debug message')
# Log an info message
logger.info('This is an info message')
# Log a warning message
logger.warning('This is a warning message')
# Log an error message
logger.error('This is an error message')
# Log a critical message
logger.critical('This is a critical message')
# Close the file handler
file_handler.close()
# Remove the file handler from the logger
logger.removeHandler(file_handler)
# Set the logging level to INFO
logger.setLevel(logging.INFO)
# Log a debug message (will not be logged because the level is set to INFO)
logger.debug('This is a debug message')
# Log an info message
logger.info('This is an info message')
# Log a warning message
logger.warning('This is a warning message')
# Log an error message
logger.error('This is an error message')
# Log a critical message
logger.critical('This is a critical message')
# Close the logger
logger.shutdown()
# The logging module allows for easy creation and management of log files for debugging and error tracking purposes.
# The basicConfig() function sets the default logging level to DEBUG, which means all messages will be logged.
# getLogger() creates a logger object with the specified name.
# The FileHandler class creates a handler that writes log messages to a file.
# The setLevel() method sets the logging level for the file handler.
# The Formatter class allows for customization of the log message format.
# The addHandler() method adds the file handler to the logger.
# The debug(), info(), warning(), error(), and critical() methods are used to log messages at different levels.
# The close() method closes the file handler.
# The removeHandler() method removes the file handler from the logger.
# The setLevel() method can also be used to change the logging level for the logger.
# The shutdown() method closes the logger and releases all resources.
Now, let’s create a logger object using the `getLogger()` function. The first argument is the name of the logger this can be anything you want (usually it’s the same as your module or script name). Here’s an example:
# Import the logging module
import logging
# Create a logger object using the getLogger() function
# The first argument is the name of the logger, which can be anything you want
# Usually, it's the same as your module or script name
logger = logging.getLogger(__name__)
The `__name__` variable holds the current module/script name, which is handy for keeping track of where logs are coming from.
Next, let’s configure our logger with a handler and formatter. A handler determines how log messages will be handled (e.g., printed to console or written to file), while a formatter specifies the format in which those messages should appear. Here’s an example:
# Import the logging module to enable logging functionality
import logging
# Import the datetime module to access current date and time
from datetime import datetime
# Create a logger object with the name of the current module
logger = logging.getLogger(__name__)
# Create a handler to specify how log messages will be handled
handler = logging.FileHandler('my_logfile.txt') # or console handler for printing to console
# Create a formatter to specify the format of log messages
formatter = logging.Formatter(f"%(asctime)s %(levelname)s {__name__}: %(message)s")
# Set the formatter for the handler
handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(handler)
# The above code sets up the logger with a handler and formatter to handle and format log messages. The logger object is named after the current module, and the handler and formatter are created to specify how log messages will be handled and formatted. The handler is then added to the logger to complete the configuration.
In this example, we’re creating a file handler (which will write logs to `my_logfile.txt`) and setting the formatter to include the date/time stamp, log level, logger name, and message itself.
Now that our logger is configured, let’s use it! We can call any of these methods on our logger object:
– debug() for logging messages with a priority level of DEBUG (lowest)
– info() for logging messages with a priority level of INFO (default)
– warning() for logging messages with a priority level of WARNING
– error() for logging messages with a priority level of ERROR
– critical() for logging messages with a priority level of CRITICAL (highest)
Here’s an example:
# Import the logging module
import logging
# Create a logger object
logger = logging.getLogger()
# Set the logging level to INFO
logger.setLevel(logging.INFO)
# Create a console handler and set the logging level to INFO
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create a formatter and add it to the console handler
formatter = logging.Formatter('%(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
# Add the console handler to the logger
logger.addHandler(console_handler)
# Log messages with different priority levels
logger.debug("This is a debug message") # This message will not be logged as the logging level is set to INFO
logger.info("This is an info message") # This message will be logged as the logging level is set to INFO
logger.warning("This is a warning message") # This message will be logged as the logging level is set to INFO
logger.error("This is an error message") # This message will be logged as the logging level is set to INFO
logger.critical("This is a critical message") # This message will be logged as the logging level is set to INFO
And that’s it! You now have logging set up in your Python application.
The `logging` module also supports configuration via file or dictionary. This can be useful if you want to change the logger settings without restarting your app (e.g., during runtime). Here are some examples:
– File Configuration:
# Import the logging module
import logging
# Import the datetime module
from datetime import datetime
# Create a logger object with the name of the current module
logger = logging.getLogger(__name__)
# Create a file handler to write logs to a file named "my_logfile.txt"
handler = logging.FileHandler('my_logfile.txt')
# Create a formatter to specify the format of the log messages
formatter = logging.Formatter(f"%(asctime)s %(levelname)s {__name__}: %(message)s")
# Set the formatter for the file handler
handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(handler)
# Configure the logger using a dictionary
logging.config.dictConfig({
'version': 1, # Specify the version of the logging configuration
'formatters': { # Define the formatters to be used
'default': { # Specify the name of the formatter
'format': '%(asctime)s %(levelname)s %(name)s: %(message)s' # Specify the format of the log messages
}
},
'handlers': { # Define the handlers to be used
'file': { # Specify the name of the handler
'class': 'logging.FileHandler', # Specify the class of the handler
'filename': 'my_logfile.txt', # Specify the name of the file to write logs to
'formatter': 'default' # Specify the formatter to be used for this handler
}
},
'root': { # Specify the root logger
'level': 'WARNING', # Set the logging level to WARNING
'handlers': ['file'] # Specify the handlers to be used for the root logger
}
})
# The logging module is now configured and ready to use!
– Dictionary Configuration:
# Dictionary Configuration:
# This is a dictionary that contains the configuration for the logging module.
import logging.config # Importing the logging.config module to configure the logging settings.
from datetime import datetime # Importing the datetime module to get the current date and time.
logger = logging.getLogger(__name__) # Creating a logger object with the name of the current module.
handler = logging.FileHandler('my_logfile.txt') # Creating a file handler to write the logs to a file named 'my_logfile.txt'.
# Alternatively, a console handler can be used to print the logs to the console.
formatter = logging.Formatter(f"%(asctime)s %(levelname)s {__name__}: %(message)s") # Creating a formatter to format the log messages.
handler.setFormatter(formatter) # Setting the formatter for the file handler.
logger.addHandler(handler) # Adding the file handler to the logger.
logging.config.fileConfig('my_log_conf.ini') # Loading the configuration from the file 'my_log_conf.ini' to configure the logger.
# This allows for easy customization of the logging settings without changing the code.
# Dictionary Configuration:
# This is a dictionary that contains the configuration for the logging module.
import logging.config # Importing the logging.config module to configure the logging settings.
from datetime import datetime # Importing the datetime module to get the current date and time.
logger = logging.getLogger(__name__) # Creating a logger object with the name of the current module.
handler = logging.FileHandler('my_logfile.txt') # Creating a file handler to write the logs to a file named 'my_logfile.txt'.
# Alternatively, a console handler can be used to print the logs to the console.
formatter = logging.Formatter(f"%(asctime)s %(levelname)s {__name__}: %(message)s") # Creating a formatter to format the log messages.
handler.setFormatter(formatter) # Setting the formatter for the file handler.
logger.addHandler(handler) # Adding the file handler to the logger.
logging.config.fileConfig('my_log_conf.ini') # Loading the configuration from the file 'my_log_conf.ini' to configure the logger.
# This allows for easy customization of the logging settings without changing the code.
In this example, we’re loading a configuration dictionary or YAML file using `dictConfig()` and `fileConfig()`, respectively.
And that’s it! We hope you found this tutorial helpful in understanding Python logging. Remember to keep calm and read the logs they can save your sanity during debugging sessions.