Asynchronous logging library with full compatibility to standard logging module
Project description
aiologging
Asynchronous logging library for Python (3.9+) with full compatibility to the standard logging module but with async methods requiring await.
Features
- Full API Compatibility: Drop-in replacement for standard logging with async methods
- Async Handlers: Non-blocking I/O for streams, files, and HTTP endpoints
- File Rotation: Size and time-based log rotation with async support
- HTTP Handlers: Send logs to HTTP endpoints with extensible authentication
- Buffered Handlers: High-performance batch processing for high-volume logging
- Performance Metrics: Built-in metrics collection for monitoring logging performance
- Error Handling: Comprehensive error handling with custom exception types
- Configuration Management: Flexible configuration from files, dictionaries, or environment variables
- Optional Dependencies: Install only what you need
- Strict Type Checking: Full mypy support with type hints
- Context Manager Support: Safe resource management with
async with
Installation
Basic Installation
pip install aiologging
With Optional Dependencies
# For file handlers
pip install aiologging[aiofiles]
# For HTTP handlers
pip install aiologging[aiohttp]
# For Protobuf support
pip install aiologging[protobuf]
# All dependencies
pip install aiologging[all]
# Development dependencies
pip install aiologging[dev]
Quick Start
Basic Usage
import asyncio
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
await logger.info("Application started")
await logger.warning("Something might be wrong")
await logger.error("An error occurred")
asyncio.run(main())
Basic Configuration
import aiologging
# Configure basic logging (similar to logging.basicConfig)
aiologging.basicConfig(
level=aiologging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
async def main():
async with aiologging.getLogger("app") as logger:
await logger.info("This will be logged to stderr")
Using Convenience Functions
import asyncio
import aiologging
async def main():
# Create handlers using convenience functions
stream_handler = aiologging.create_stream_handler(level=aiologging.INFO)
file_handler = aiologging.create_file_handler("app.log", level=aiologging.DEBUG)
# Get logger and add handlers
async with aiologging.getLogger("app") as logger:
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
await logger.info("This goes to both stderr and app.log")
asyncio.run(main())
Examples
Complete runnable examples live in the examples/ directory:
- examples/basic_usage.py — levels,
basicConfig,exc_info, one-off logging - examples/file_logging.py — file handler, size- and time-based rotation
- examples/http_logging.py — JSON batches over HTTP with a custom authenticator (includes a local test collector)
- examples/config_usage.py — configuring loggers from a dictionary
Handlers
Stream Handler
import aiologging
import sys
async def main():
async with aiologging.getLogger("app") as logger:
# Add stdout handler
stdout_handler = aiologging.AsyncStreamHandler(sys.stdout)
logger.addHandler(stdout_handler)
await logger.info("This goes to stdout")
File Handler (requires aiofiles)
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
# Add file handler
file_handler = aiologging.AsyncFileHandler("app.log")
logger.addHandler(file_handler)
await logger.info("This goes to app.log")
Rotating File Handler (requires aiofiles)
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
# Size-based rotation
rotating_handler = aiologging.AsyncRotatingFileHandler(
"app.log",
max_bytes=1024*1024, # 1MB
backup_count=5
)
logger.addHandler(rotating_handler)
# Time-based rotation
timed_handler = aiologging.AsyncTimedRotatingFileHandler(
"app.log",
when="midnight",
backup_count=7
)
logger.addHandler(timed_handler)
await logger.info("This will be rotated")
HTTP Handler (requires aiohttp)
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
# Basic HTTP handler
http_handler = aiologging.AsyncHttpHandler(
"https://api.example.com/logs",
headers={"Authorization": "Bearer token"}
)
logger.addHandler(http_handler)
await logger.info("This will be sent via HTTP")
Custom Authentication
import aiologging
async def oauth_authenticator(session, request_data):
"""Custom OAuth authentication."""
# Refresh token logic here
token = await refresh_oauth_token()
return {"Authorization": f"Bearer {token}"}
async def main():
async with aiologging.getLogger("app") as logger:
http_handler = aiologging.AsyncHttpHandler(
"https://api.example.com/logs",
authenticator=oauth_authenticator
)
logger.addHandler(http_handler)
await logger.info("This uses custom authentication")
HTTP Handler Formats
JSON Handler
json_handler = aiologging.AsyncHttpJsonHandler(
"https://api.example.com/logs"
)
Text Handler
text_handler = aiologging.AsyncHttpTextHandler(
"https://api.example.com/logs"
)
Protobuf Handler (requires protobuf)
proto_handler = aiologging.AsyncHttpProtoHandler(
"https://api.example.com/logs"
)
Universal Handler (auto-detect format)
universal_handler = aiologging.AsyncHttpHandler(
"https://api.example.com/logs",
format_type="application/json" # Optional: auto-detected if not specified
)
Advanced Usage
Custom Filters
import aiologging
class CustomFilter:
def filter(self, record):
return "important" in record.getMessage()
async def main():
async with aiologging.getLogger("app") as logger:
logger.addFilter(CustomFilter())
await logger.info("This is important") # Will be logged
await logger.info("This is not") # Will be filtered
Custom Formatters
import logging
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
formatter = logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s"
)
handler = aiologging.AsyncStreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
await logger.info("Formatted message")
Error Handling
import aiologging
async def error_handler(record, exception):
"""Custom error handler for failed log operations."""
print(f"Failed to log {record.getMessage()}: {exception}")
async def main():
async with aiologging.getLogger("app") as logger:
handler = aiologging.AsyncStreamHandler()
handler.error_handler = error_handler
logger.addHandler(handler)
await logger.info("This has custom error handling")
Performance Metrics
import asyncio
import aiologging
async def main():
# Create logger with metrics enabled
async with aiologging.getLogger("app") as logger:
# Get metrics for the logger
metrics = logger.get_metrics()
print(f"Logger metrics: {metrics}")
# Get metrics for handlers
for handler in logger.handlers:
if hasattr(handler, 'get_metrics'):
handler_metrics = handler.get_metrics()
print(f"Handler metrics: {handler_metrics}")
asyncio.run(main())
Batch Processing
import aiologging
from aiologging.types import BatchConfig
async def main():
async with aiologging.getLogger("app") as logger:
# Configure batch processing for HTTP handler
batch_config = BatchConfig(
batch_size=100,
flush_interval=5.0,
max_retries=3
)
http_handler = aiologging.AsyncHttpHandler(
"https://api.example.com/logs",
batch_config=batch_config
)
logger.addHandler(http_handler)
# Log many messages - they'll be sent in batches
for i in range(150):
await logger.info(f"Message {i}")
Configuration Management
Configuration from Dictionary
import asyncio
import aiologging
config = {
"version": 1,
"loggers": {
"myapp": {
"level": "INFO",
"handlers": ["console", "file"]
}
},
"handlers": {
"console": {
"class": "stream",
"level": "INFO",
"stream": "stdout"
},
"file": {
"class": "file",
"level": "DEBUG",
"filename": "app.log",
"mode": "a"
}
}
}
# Configure from dictionary
aiologging.configure_from_dict(config)
async def main():
logger = aiologging.get_configured_logger("myapp")
await logger.info("This uses configured logger")
asyncio.run(main())
Configuration from File
import asyncio
import aiologging
# Configure from JSON file
aiologging.configure_from_file("logging_config.json")
async def main():
logger = aiologging.get_configured_logger("myapp")
await logger.info("This uses logger configured from file")
asyncio.run(main())
Migration from Standard Logging
Standard Logging
import logging
logger = logging.getLogger("app")
logger.info("Message")
With aiologging
import aiologging
async def main():
async with aiologging.getLogger("app") as logger:
await logger.info("Message")
Key Differences
- Async Context: Use
async withfor proper resource management - Await Methods: All logging methods require
await - Async Handlers: All handlers are non-blocking
- Type Safety: Full type hints and mypy compliance
- Error Handling: Enhanced error handling with custom exception types
- Performance Metrics: Built-in metrics collection for monitoring
API Reference
Logger Methods
All standard logging methods are available as async:
await logger.log(level, msg, *args, **kwargs)await logger.debug(msg, *args, **kwargs)await logger.info(msg, *args, **kwargs)await logger.warning(msg, *args, **kwargs)await logger.error(msg, *args, **kwargs)await logger.critical(msg, *args, **kwargs)await logger.exception(msg, *args, **kwargs)
Handler Classes
AsyncHandler- Base async handlerAsyncStreamHandler- Stream output handlerAsyncFileHandler- File output handler (requires aiofiles)AsyncRotatingFileHandler- Size-based rotation (requires aiofiles)AsyncTimedRotatingFileHandler- Time-based rotation (requires aiofiles)AsyncHttpHandler- Universal HTTP handler (requires aiohttp)AsyncHttpTextHandler- Plain text HTTP handler (requires aiohttp)AsyncHttpJsonHandler- JSON HTTP handler (requires aiohttp)AsyncHttpProtoHandler- Protobuf HTTP handler (requires aiohttp, protobuf)
Configuration Classes
BatchConfig- Batch processing configurationFileConfig- File handler configurationHttpConfig- HTTP handler configurationLoggerConfig- Logger configurationRotationConfig- Rotation configuration for file handlers
Exception Classes
AiologgingError- Base exception for all aiologging errorsHandlerError- Base exception for handler errorsConfigurationError- Configuration-related errorsDependencyError- Missing optional dependenciesAuthenticationError- Authentication failuresNetworkError- Network-related errorsFileError- File operation errorsRotationError- File rotation errorsBatchError- Batch processing errorsFormatterError- Formatting errorsLoggerError- Logger operation errorsContextError- Context manager errors
Performance Considerations
- Buffering: Use buffered handlers for high-volume logging
- Batch Processing: Configure appropriate batch sizes for HTTP handlers
- Async I/O: All I/O operations are non-blocking
- Resource Management: Always use context managers for proper cleanup
- Metrics Collection: Enable metrics to monitor performance
- Rate Limiting: Use rate limiters to prevent log flooding
- Adaptive Buffering: Enable adaptive buffering for optimal performance
Testing
# Install development dependencies
pip install aiologging[dev]
# Run tests
pytest
# Run tests with coverage
pytest --cov=aiologging
# Run type checking
mypy aiologging
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass and type checking succeeds
- Submit a pull request
License
MIT License - see LICENSE file for details.
Changelog
0.1.0
- Initial release
- Full async logging API
- Stream, file, and HTTP handlers
- File rotation support
- Extensible authentication
- Optional dependencies
- Strict type checking
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiologging-0.1.0.tar.gz.
File metadata
- Download URL: aiologging-0.1.0.tar.gz
- Upload date:
- Size: 70.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37fa05808bfed9ca083e946bab9b4636bded359df2689b912c7211d1dac37d3d
|
|
| MD5 |
f84429b9cdd4f0f0427c1926ff18ebe1
|
|
| BLAKE2b-256 |
76a2aee5915b62fb5b7b066ba3baef7dd68425b65b3f254432dae35d37ee42dc
|
File details
Details for the file aiologging-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aiologging-0.1.0-py3-none-any.whl
- Upload date:
- Size: 51.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1cb5fa3853be9a16b08253be2b7c65490ce63d379e0caa0f2b6ff6df0d70d787
|
|
| MD5 |
9ca19210fb38c800019706d98faff98c
|
|
| BLAKE2b-256 |
3bb087be0e6b1e0ea8fab35a1a3b91a6dc0a5ae9cbd8d83c5a98358d52ac2e7f
|