A robust, scalable Python logging library that enables distributed log collection with advanced connection management, automatic reconnection, and thread-safe logging capabilities. π
- π§΅ Thread-safe logging client with automatic reconnection
- β³ Exponential backoff retry mechanism
- π©Ί Connection health monitoring
- π¨ Durable message delivery
- βοΈ Scalable message consumption
- π¦ Batch processing capabilities
- π Error handling and recovery
- π» Multi-threaded architecture
- Thread-Safe Operations:
- π Thread-safe logging with mutex locks
- π₯ Concurrent access handling
- β Safe connection management
- Robust Connection Management:
- π Automatic reconnection with exponential backoff
- π©Ί Connection health monitoring
- β±οΈ Configurable heartbeat (600 seconds)
- β³ Connection timeout protection (300 seconds)
- π Socket timeout (10 seconds)
- Reliable Message Delivery:
- π Durable message queues
- πΎ Message persistence
- β Delivery confirmation
- π Automatic retry on failure
- Flexible Log Routing:
- π οΈ Service-specific routing
- π Log level-based queues
- π§© Dynamic queue declaration
- π Direct exchange support
- Advanced Message Queue Management:
- β³ Configurable message TTL (7 days default) - Messages automatically expire after a set time period to prevent queue overflow.
- π Maximum queue length limits - Set hard limits on queue size to protect system resources and maintain performance.
- πͺ Backpressure handling - Automatically manages message flow when the system is under heavy load to prevent crashes.
- Scalable Processing:
- π§΅ Multi-threaded message processing - Parallel processing of messages across multiple threads for improved throughput.
- π¦ Batch processing support - Groups messages into batches for efficient bulk processing and reduced database load.
- βοΈ Configurable worker pool - Adjust the number of worker threads based on your system's capacity and requirements.
- Error Recovery:
- π₯ Failure backoff queue - Stores failed messages separately for retry with exponential backoff to prevent system overload.
- π Automatic retry mechanism - Intelligently retries failed operations with configurable attempts and delays.
- β JSON validation - Ensures message integrity by validating JSON structure before processing to prevent data corruption.
pip install loghiveCreate a config.env file for the service to fetch the connection parameters for rabbitmq, rabbitmq and email connection
# Basic Configurations
LOG_LEVEL=DEBUG
# Database Configurations
POSTGRES_DB_HOST=localhost
POSTGRES_DB_USER=***
POSTGRES_DB_NAME=***
POSTGRES_DB_PASSWORD=***
POSTGRES_DB_PORT=***
# RabbitMQ & Consumer Configurations
QUEUE_HOST=localhost
QUEUE_USER=***
QUEUE_PASSWORD=***
QUEUE_PORT=***
QUEUE_MAX_SIZE=10000000
# Consumer
CONSUMER_BATCH_SIZE=1000
# Monitoring
ENABLE_EMAIL_MONITORING=False
EMAIL_HOST=***
EMAIL_PORT=***
EMAIL_SENDER_EMAIL=***
EMAIL_SENDER_PASSWORD=***
from loghive.logger.rabbitmqlogger import LoggerClient
# Initialize the logger
logger = LoggerClient(
service_name="my-service",
rabbitmq_url="amqp://localhost:5672/"
)
# Log messages with different levels
logger.log("INFO", "User logged in", {"user_id": "123"})
logger.log("ERROR", "Database connection failed", {"retry_count": 3})
logger.log("WARNING", "High memory usage", {"usage_percent": 85})from loghive.consumer.rabbitmqconsumer import start_consumer
from loghive.main.settings import internal_logger
try:
start_consumer(["flask_service"]) # replace with your service names
except Exception as e:
internal_logger.error(f"Error faced while starting consumer: {e}")The internal_logger can be imported from loghive.main.settings, this will be behave like a normal logger and will
not be publishing the message to rabbitmq.
{
"service": "service_name",
"level": "INFO",
"message": "Log message",
"information": {
"# Additional context as dictionary"
},
"timestamp": "2024-12-27 10:30:45"
}connection_params = {
"heartbeat": 600, # Heartbeat interval in seconds
"blocked_connection_timeout": 300, # Connection timeout in seconds
"socket_timeout": 10, # Socket timeout in seconds
}QUEUE_ARGUMENTS = {
"x-message-ttl": 604800000, # 7 days in milliseconds
"x-max-length": 1000000, # Maximum queue size
}+----------------+ +------------------+ +----------------+
| Application | | LoggerClient | | RabbitMQ |
| Code | --> | - Thread Safety | --> | Exchange |
| | | - Auto Reconnect | | (Direct) |
+----------------+ | - Retry Logic | +----------------+
+------------------+
1. Application generates log
β
2. LoggerClient validates and formats message
β
3. Thread-safe connection check
β
4. Publish with retry mechanism
β
5. RabbitMQ confirms delivery
β
6. Consumer processes message
- π Connection failures trigger automatic reconnection
- β³ Exponential backoff between retry attempts (1-30 seconds)
- π« Maximum of 3 retry attempts per operation
- π©Ί Separate monitoring thread for connection health
- π Thread-safe operation handling
- π Durable queues and exchanges
- πΎ Persistent messages (delivery_mode=2)
- β Message acknowledgment
- π οΈ Automatic queue declaration
- π Connection recovery
- Initialization:
logger = LoggerClient(
service_name="unique-service-name",
rabbitmq_url="amqp://username:password@host:port/vhost"
)- Graceful Shutdown:
# Always close the logger when done
logger.close()- Error Handling:
try:
# Your application code
logger.log("INFO", "Operation successful")
except Exception as e:
logger.log("ERROR", "Operation failed", {"error": str(e)})- Structured Logging:
logger.log(
"INFO",
"User action completed",
{
"user_id": "123",
"action": "checkout",
"duration_ms": 150
}
)The logger provides built-in monitoring for:
- π©Ί Connection status
- β Message delivery success/failure
- π Retry attempts
- π Queue health
- π§΅ Thread status
- π§΅ Thread-safe operations may impact throughput
- π©Ί Connection monitoring adds minimal overhead
- π Retry mechanisms prevent message loss
- β±οΈ Heartbeat monitoring ensures connection health
- π« Socket timeouts prevent hanging operations
See our Contributing Guide for details on how to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and help:
- π Check the documentation
- π Review existing issues
- π Create a new issue with detailed reproduction steps