Skip to main content

A reusable Django app for asynchronous, thread-safe logging with rich metadata, admin interface, and API support.

Project description

Django Async Logger

A reusable Django app that provides asynchronous logging functionality using a separate thread to avoid blocking the main application.

Features

  • Asynchronous Logging: All log operations run in a separate thread
  • Thread-Safe: Uses a queue system for thread-safe logging
  • Rich Metadata: Captures module, function, line number, user ID, request ID, and extra data
  • Admin Interface: Django admin interface for viewing and managing logs
  • API Endpoints: REST API for external logging
  • Middleware: Automatic request logging with unique request IDs
  • Decorators: Utility decorators for function logging and performance monitoring
  • Context Managers: Easy-to-use context managers for operation logging
  • Configurable: Customizable queue size, flush intervals, and cleanup policies

Installation

  1. Add the app to your Django project:
INSTALLED_APPS = [
    # ...
    'logq',
]
  1. Add the middleware to your settings:
MIDDLEWARE = [
    # ...
    'logq.middleware.AsyncLoggingMiddleware',
]
  1. Run migrations:
python manage.py makemigrations logq
python manage.py migrate
  1. (Optional) Configure logging settings:
ASYNC_LOGGING_CONFIG = {
    'MAX_QUEUE_SIZE': 1000,
    'FLUSH_INTERVAL': 1.0,  # seconds
    'AUTO_CLEANUP_DAYS': 30,
    'ENABLE_REQUEST_LOGGING': True,
    'IGNORE_PATHS': ['/admin/'],  # paths to ignore for request logging
}

Usage

Basic Logging

from logq.async_logger import get_async_logger

logger = get_async_logger()

# Different log levels
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")

# With extra data
logger.info("User action", extra_data={'action': 'login', 'ip': '192.168.1.1'})

# Log exceptions
try:
    # some code that might fail
    pass
except Exception as e:
    logger.exception("An error occurred", exc_info=str(e))

Function Decorators

from logq.utils import log_function_call, log_performance

@log_function_call
def my_function():
    return "result"

@log_function_call(level='DEBUG')
def debug_function():
    return "debug result"

@log_performance(threshold_seconds=0.5)
def slow_function():
    time.sleep(1)
    return "slow result"

Context Managers

from logq.utils import LogContext

with LogContext("Processing data", level='INFO'):
    # do some work
    time.sleep(0.1)
    # automatically logs start and completion with timing

API Logging

import requests
import json

# Log via API
data = {
    'level': 'INFO',
    'message': 'External log message',
    'extra_data': {'source': 'external_service'}
}

response = requests.post(
    'http://your-domain/logq/api/log/',
    data=json.dumps(data),
    headers={'Content-Type': 'application/json'}
)

# Retrieve logs via API
response = requests.get('http://your-domain/logq/api/logs/?limit=10')
logs = response.json()['logs']

Admin Interface

Access the admin interface at /admin/ to view and manage logs. Features include:

  • Filter by level, module, timestamp, user ID
  • Search by message, module, function, request ID

Management Commands

Clean old logs:

# Delete logs older than 30 days
python manage.py clean_logs

# Delete logs older than 7 days
python manage.py clean_logs --days 7

# Delete only DEBUG and INFO logs older than 30 days
python manage.py clean_logs --level INFO

# Dry run to see what would be deleted
python manage.py clean_logs --dry-run

Configuration Options

Setting Default Description
MAX_QUEUE_SIZE 1000 Maximum number of log entries in the queue
FLUSH_INTERVAL 1.0 How often to flush logs to database (seconds)
AUTO_CLEANUP_DAYS 30 Days to keep logs before auto-cleanup
ENABLE_REQUEST_LOGGING True Whether to log all HTTP requests

Model Fields

The LogEntry model includes:

  • timestamp: When the log was created
  • level: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  • message: The log message
  • module: Python module where the log originated
  • function: Function name where the log originated
  • line_number: Line number where the log originated
  • user_id: ID of the user (if authenticated)
  • request_id: Unique request identifier
  • extra_data: Additional JSON data
  • created_at: When the entry was saved to database

Performance Considerations

  • The logger runs in a separate thread and won't block your main application
  • Log entries are batched and written to the database periodically
  • If the queue is full, new entries are dropped (with console fallback)
  • Consider setting up database indexes for better query performance
  • Use the cleanup command regularly to prevent database bloat

Thread Safety

The logger is completely thread-safe:

  • Uses a thread-safe queue for communication
  • Database operations are wrapped in transactions
  • Multiple threads can safely call the logger simultaneously

Customization

You can extend the logger by:

  1. Creating custom log levels
  2. Adding new fields to the LogEntry model
  3. Customizing the admin interface
  4. Adding new API endpoints
  5. Creating custom middleware

Troubleshooting

Logs not appearing

  • Check that the async logger thread is running
  • Verify database migrations are applied
  • Check for any database connection issues

Performance issues

  • Reduce FLUSH_INTERVAL for more frequent writes
  • Increase MAX_QUEUE_SIZE for higher throughput
  • Add database indexes for frequently queried fields

Memory usage

  • Reduce MAX_QUEUE_SIZE if memory is a concern
  • Run cleanup commands more frequently
  • Monitor database size and clean old logs

License

This project is open source and available under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djlogq-1.0.4.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djlogq-1.0.4-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file djlogq-1.0.4.tar.gz.

File metadata

  • Download URL: djlogq-1.0.4.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for djlogq-1.0.4.tar.gz
Algorithm Hash digest
SHA256 2c04a4cbb9758fffe7b2e0db58f26c03b245b2505f9e324d6cf28abc0b9e47f7
MD5 e659c34d49e17b52e8d64ae15c765a4c
BLAKE2b-256 efbe9aa7e29c256630622b71f9a185a7ac6d28adcbcbb8089503bc4d247bcab1

See more details on using hashes here.

File details

Details for the file djlogq-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: djlogq-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for djlogq-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 64cfe179e4286f6b9598dded47bde87c2300315e4d57e4e74c7b51019adf916e
MD5 dc58894d41da46f629ae3f790d698d05
BLAKE2b-256 3aa876b2164377415c96f35001f8bcf73c8190d4df0ad57c136062b01cf4a145

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page