Logutils: Legacy Logging Utilities for Python
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
-
ModuleNotFoundError: No module named 'logutils'
cause `logutils` is not part of Python's standard library and needs to be installed, or you are trying to use a feature that is now in `logging`.fixIf you genuinely need `logutils` (highly discouraged), run `pip install logutils`. Otherwise, migrate your code to use the standard `logging` module and its submodules (e.g., `logging.handlers`). -
AttributeError: module 'logging' has no attribute 'QueueHandler'
cause You are attempting to access `QueueHandler` directly from the `logging` module, but it resides in `logging.handlers`.fixChange your import statement from `import logging` to `from logging.handlers import QueueHandler`. If you were trying to use `logutils.queue.QueueHandler`, migrate to `logging.handlers.QueueHandler`.
Warnings
- breaking The `logutils` library is abandoned and has not been updated since 2012. It may have compatibility issues or rely on Python 2.x specific behaviors when run on modern Python 3.x environments.
- gotcha Many features originally provided by `logutils` (e.g., `WatchedFileHandler`, `TimedRotatingFileHandler`, `QueueHandler`) have been integrated into Python's standard `logging` module since Python 2.6 and 3.1, or `logging.handlers` since Python 3.2.
- deprecated The maintainers of `logutils` are no longer active, and the project's official website is defunct. There are no ongoing security updates or bug fixes.
Install
-
pip install logutils
Imports
- QueueHandler
from logutils.queue import QueueHandler
- QueueListener
from logutils.queue import QueueListener
Quickstart
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()