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)

Python logging module Tutorial

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:

  • Import the logging module:
import logging
  • Basic logging configuration: Use the 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")
  • Log messages with different levels: Use the logging functions (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.

  • Log messages with variable data: Use placeholders in your log messages to include variable data.
name = "Alice"
age = 30
logging.info("User %s is %d years old", name, age)
  • Configure logging to output to a file: Use the 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")
  • Configure logging with multiple loggers:
# 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.

  1. How to use the logging module in Python:

    • Description: The logging module is a built-in Python module that provides flexible logging of messages in your code.
    • Code:
      import logging
      
      logging.basicConfig(level=logging.INFO)
      logging.info("This is an info message")
      
  2. Logging levels in Python and when to use them:

    • Description: Logging levels allow you to categorize messages by severity. Common levels include DEBUG, INFO, WARNING, ERROR, and CRITICAL.
    • Code:
      import logging
      
      logging.debug("Debugging message")
      logging.info("Informational message")
      logging.warning("Warning message")
      logging.error("Error message")
      logging.critical("Critical error message")
      
  3. Configuring logging in Python applications:

    • Description: Configure the logging module to customize the logging behavior, such as setting the log level or specifying a log file.
    • Code:
      import logging
      
      logging.basicConfig(filename='app.log', level=logging.DEBUG)
      logging.info("Log message to file")
      
  4. Logging to different outputs with Python logging:

    • Description: You can log messages to various outputs, such as the console or a file.
    • Code:
      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")
      
  5. Rotating logs with the Python logging module:

    • Description: Rotating logs helps manage log file sizes by creating new log files or deleting old ones.
    • Code:
      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")
      
  6. Customizing log formats in Python logging:

    • Description: Customize the log message format to include information like timestamps, log levels, and the actual log message.
    • Code:
      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")
      
  7. Logging exceptions in Python with the logging module:

    • Description: Use the logging module to catch and log exceptions.
    • Code:
      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)
      
  8. Filtering log messages in Python logging:

    • Description: Apply filters to selectively log messages based on specified criteria.
    • Code:
      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")
      
  9. Asynchronous logging in Python:

    • Description: Perform logging asynchronously to avoid blocking the main program execution.
    • Code:
      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")
      
  10. Logging in a multi-threaded or multi-process environment:

    • Description: Handle logging in scenarios where multiple threads or processes are involved.
    • Code:
      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")
      
  11. Integrating logging with Python frameworks (e.g., Django):

    • Description: Integrate the logging module with popular Python frameworks like Django.
    • Code:
      import logging
      from django.http import HttpResponse
      
      logger = logging.getLogger(__name__)
      
      def my_view(request):
          logger.info("Processing request")
          return HttpResponse("Response")
      
  12. Using logging for debugging in Python:

    • Description: Utilize logging for debugging purposes by adding informative messages at critical points in your code.
    • Code:
      import logging
      
      logging.basicConfig(level=logging.DEBUG)
      
      def my_function():
          logging.debug("Entering my_function")
          # Some code
          logging.debug("Exiting my_function")
      
      my_function()
      
  13. Security considerations for Python logging:

    • Description: Be cautious about logging sensitive information, and consider security best practices to protect log data.
    • Code:
      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")