Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
The logging
module in Python is a powerful and flexible built-in library for handling logging in your applications. Logging is crucial for tracking the execution of your code, monitoring its behavior, and debugging issues. This tutorial will provide an introduction to using the logging
module in Python:
logging
module:import logging
basicConfig()
function to set up the basic configuration for the logging system. You can specify the minimum logging level, log format, output file, and other options.logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
debug()
, info()
, warning()
, error()
, and critical()
) to log messages with different levels of importance.logging.debug("This is a debug message") logging.info("This is an info message") logging.warning("This is a warning message") logging.error("This is an error message") logging.critical("This is a critical message")
With the configuration above, only messages with a level of INFO
and higher will be displayed.
name = "Alice" age = 30 logging.info("User %s is %d years old", name, age)
filename
parameter in the basicConfig()
function to log messages to a file.logging.basicConfig(filename="app.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
# Create a custom logger logger = logging.getLogger("my_logger") logger.setLevel(logging.DEBUG) # Create handlers console_handler = logging.StreamHandler() file_handler = logging.FileHandler("app.log") console_handler.setLevel(logging.WARNING) file_handler.setLevel(logging.DEBUG) # Create formatters console_format = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") file_format = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") # Add formatters to handlers console_handler.setFormatter(console_format) file_handler.setFormatter(file_format) # Add handlers to the logger logger.addHandler(console_handler) logger.addHandler(file_handler) # Log messages using the custom logger logger.debug("This is a debug message") logger.info("This is an info message") logger.warning("This is a warning message") logger.error("This is an error message") logger.critical("This is a critical message")
In this example, the custom logger logs messages to both the console and a file. The console displays messages with a level of WARNING
and higher, while the file contains messages with a level of DEBUG
and higher.
In this tutorial, you learned the basics of using the logging
module in Python, including configuring logging, logging messages with different levels, logging messages with variable data, logging to a file, and using multiple loggers. By incorporating logging into your applications, you can more easily monitor, track, and debug your code.
How to use the logging module in Python:
import logging logging.basicConfig(level=logging.INFO) logging.info("This is an info message")
Logging levels in Python and when to use them:
import logging logging.debug("Debugging message") logging.info("Informational message") logging.warning("Warning message") logging.error("Error message") logging.critical("Critical error message")
Configuring logging in Python applications:
import logging logging.basicConfig(filename='app.log', level=logging.DEBUG) logging.info("Log message to file")
Logging to different outputs with Python logging:
import logging console_handler = logging.StreamHandler() file_handler = logging.FileHandler('app.log') logging.basicConfig(level=logging.INFO, handlers=[console_handler, file_handler]) logging.info("Log message to console and file")
Rotating logs with the Python logging module:
import logging from logging.handlers import RotatingFileHandler rotating_handler = RotatingFileHandler('app.log', maxBytes=1024, backupCount=3) logging.basicConfig(level=logging.INFO, handlers=[rotating_handler]) logging.info("Log message with rotating logs")
Customizing log formats in Python logging:
import logging formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler = logging.StreamHandler() handler.setFormatter(formatter) logging.basicConfig(level=logging.INFO, handlers=[handler]) logging.info("Custom log format")
Logging exceptions in Python with the logging module:
import logging try: # Some code that may raise an exception raise ValueError("An example exception") except Exception as e: logging.error("Exception occurred", exc_info=True)
Filtering log messages in Python logging:
import logging class CustomFilter(logging.Filter): def filter(self, record): return record.levelno == logging.INFO logging.basicConfig(level=logging.DEBUG) handler = logging.StreamHandler() handler.addFilter(CustomFilter()) logging.getLogger().addHandler(handler) logging.info("This will be logged") logging.debug("This will not be logged")
Asynchronous logging in Python:
import logging from concurrent.futures import ThreadPoolExecutor logging.basicConfig(level=logging.INFO) executor = ThreadPoolExecutor(max_workers=5) def log_message(message): logging.info(message) executor.submit(log_message, "Asynchronous log message")
Logging in a multi-threaded or multi-process environment:
import logging from concurrent.futures import ThreadPoolExecutor logging.basicConfig(level=logging.INFO) executor = ThreadPoolExecutor(max_workers=5) def log_message(message): logging.info(message) # Example in a multi-threaded environment executor.submit(log_message, "Log message from thread")
Integrating logging with Python frameworks (e.g., Django):
import logging from django.http import HttpResponse logger = logging.getLogger(__name__) def my_view(request): logger.info("Processing request") return HttpResponse("Response")
Using logging for debugging in Python:
import logging logging.basicConfig(level=logging.DEBUG) def my_function(): logging.debug("Entering my_function") # Some code logging.debug("Exiting my_function") my_function()
Security considerations for Python logging:
import logging import logging.handlers # Use secure log handlers for sensitive data secure_handler = logging.handlers.SysLogHandler() logging.basicConfig(level=logging.INFO, handlers=[secure_handler]) logging.info("Secure log message")