Logutils: Legacy Logging Utilities for Python

0.3.5 · abandoned · verified Thu Apr 16

Logutils is a collection of logging utilities designed for Python. Its last release (0.3.5) was in 2012, and the library is no longer actively maintained. Many of the features it provided, such as `QueueHandler` and `WatchedFileHandler`, have since been integrated into Python's standard `logging` module or are available via more modern, actively maintained libraries.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how the `logutils.queue.QueueHandler` and `logutils.queue.QueueListener` *used* to be used for asynchronous logging. It is provided for historical context only. Modern Python applications should leverage the `logging.handlers.QueueHandler` and `logging.handlers.QueueListener` (available since Python 3.2), or other concurrent logging patterns, from the standard library.

import logging
from logutils.queue import QueueHandler, QueueListener
import queue
import time

# --- WARNING: This code uses the abandoned logutils library. ---
# --- It is provided for historical context only.             ---
# --- Modern applications should use the standard 'logging' module. ---

# Setup a basic handler for the listener to write to
file_handler = logging.FileHandler('legacy_app.log')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)

# Create a queue for logs
log_queue = queue.Queue(-1)

# Create a QueueHandler to put messages into the queue
queue_handler = QueueHandler(log_queue)

# Create a QueueListener to pull messages from the queue and send to the file handler
# Note: QueueListener from logutils is deprecated; logging.handlers.QueueListener is preferred.
queue_listener = QueueListener(log_queue, file_handler)

# Get a logger and add the queue handler
logger = logging.getLogger('legacy_app')
logger.setLevel(logging.INFO)
logger.addHandler(queue_handler)

# Start the listener thread
queue_listener.start()

try:
    logger.info('This is an info message from logutils.')
    logger.warning('A warning message via logutils.')
    time.sleep(0.1)
finally:
    # Stop the listener gracefully
    queue_listener.stop()
    print('Log messages processed (check legacy_app.log if file was written).')
    # In a real scenario, you'd ensure the file handler is closed properly.

# --- Modern Python Alternative Example (conceptual) ---
# from logging.handlers import QueueHandler, QueueListener
# import logging.config
# import queue

# q = queue.Queue(-1)
# queue_handler = QueueHandler(q)

# listener = QueueListener(q, logging.FileHandler('modern_app.log'))
# listener.start()
# # ... logging code ...
# listener.stop()

view raw JSON →