This guide documents the migration from scattered logging.basicConfig() calls to the centralized LoggingManager system in autobot-backend/utils/logging_manager.py.
Issue: #42 - Logging Configuration Standardization
- Configuration Conflicts - Multiple calls overwrite each other
- No Categorization - All logs mixed together, hard to filter
- No Rotation - Log files grow unbounded
- Inconsistent Formatting - Different formats across codebase
- No Environment Control - Can't configure via env vars
- ✅ Single Configuration Point -
autobot-backend/utils/logging_manager.py - ✅ Category-Based Loggers - backend, frontend, llm, debug, audit
- ✅ Automatic Log Rotation - 10MB max size, 5 backups
- ✅ Environment-Based Config -
AUTOBOT_LOG_LEVEL,AUTOBOT_LOGS_DIR - ✅ Consistent Formatting - Same format across all modules
- ✅ Backward Compatible - Convenience functions for easy migration
Migrated Files:
- ✅
backend/main.py - ✅
backend/app_factory_enhanced.py - ✅
backend/utils/redis_compatibility.py - ✅
src/project_state_manager.py - ✅
autobot-backend/agents/research_agent.py - ✅
autobot-backend/utils/system_context.py
import logging
# Module-level logging setup
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# ... code ...
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
# ... test code ...from src.utils.logging_manager import get_logger
# Get centralized logger
logger = get_logger(__name__, "backend")
# ... code ...
if __name__ == "__main__":
# Logging configured via centralized logging_manager
# ... test code ...Remove:
import loggingAdd:
from src.utils.logging_manager import get_loggerReplace:
logger = logging.getLogger(__name__)With:
logger = get_logger(__name__, "backend") # or appropriate categoryRemove all instances of:
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG, format="...")
logging.basicConfig(...)Replace with comment:
# Logging configured via centralized logging_managerSelect the correct category for your logger:
| Category | Use Case | Example Modules |
|---|---|---|
backend |
Backend API, services, utilities | autobot-backend/api/*.py, autobot-backend/utils/*.py |
frontend |
Frontend-related backend code | Vue build scripts, frontend helpers |
llm |
LLM interactions, prompts | src/llm_interface.py, LLM services |
debug |
Debug/development logging | Test utilities, debug scripts |
audit |
Security audit logs | Security layer, auth modules |
Before:
import logging
logger = logging.getLogger(__name__)
def my_function():
logger.info("Processing data")After:
from src.utils.logging_manager import get_logger
logger = get_logger(__name__, "backend")
def my_function():
logger.info("Processing data")Before:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)After:
from src.utils.logging_manager import get_logger
logger = get_logger(__name__, "backend")Before:
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
main()After:
if __name__ == "__main__":
# Logging configured via centralized logging_manager
main()Before:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
test_logger = logging.getLogger("test")
def test_function():
logger.debug("Test starting")
test_logger.info("Running tests")After:
from src.utils.logging_manager import get_logger
logger = get_logger(__name__, "debug")
test_logger = get_logger("test", "debug")
def test_function():
logger.debug("Test starting")
test_logger.info("Running tests")Before:
import asyncio
import logging
logger = logging.getLogger(__name__)
async def async_task():
logger.info("Async task started")After:
import asyncio
from src.utils.logging_manager import get_logger
logger = get_logger(__name__, "backend")
async def async_task():
logger.info("Async task started")# Check that old logging imports are removed
grep -r "import logging" backend/ src/ --include="*.py"
# Should only see centralized logging_manager imports
grep -r "from src.utils.logging_manager import get_logger" backend/ src/# Check for remaining basicConfig calls (excluding logging_manager.py itself)
grep -r "logging.basicConfig" backend/ src/ --include="*.py" \
--exclude="logging_manager.py"# Test script to verify logging works
from src.utils.logging_manager import get_logger
logger = get_logger("test_migration", "debug")
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")Expected output in logs/backend.log:
2025-01-14 10:30:00 - test_migration - DEBUG - Debug message
2025-01-14 10:30:00 - test_migration - INFO - Info message
2025-01-14 10:30:00 - test_migration - WARNING - Warning message
2025-01-14 10:30:00 - test_migration - ERROR - Error message
# Check that log files are created with rotation
ls -lh logs/
# Should see: backend.log, backend.log.1, backend.log.2, etc.Control logging via environment variables:
# Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
export AUTOBOT_LOG_LEVEL=DEBUG
# Set logs directory
export AUTOBOT_LOGS_DIR=/path/to/logs
# Set backup directory
export AUTOBOT_LOGS_BACKUP_DIR=/path/to/backupsPattern for scripts:
- Replace
logging.basicConfig()with centralized logger - Use
debugcategory for most scripts - Keep print statements for user-facing output
Examples:
scripts/detect-hardcoded-values.sh # Shell script - no migration needed
scripts/test_*.py # Use "debug" category
scripts/utilities/*.py # Use "debug" categoryPattern for tests:
- Use
debugcategory for test logging - Keep test framework logging separate
- Only migrate test utilities, not test assertions
Examples:
tests/unit/*.py # Use "debug" category
tests/integration/*.py # Use "debug" category# Find all files with logging.basicConfig
find backend/ src/ scripts/ tests/ -name "*.py" -exec grep -l "logging.basicConfig" {} \;
# Count remaining migrations
grep -r "logging.basicConfig" backend/ src/ scripts/ tests/ --include="*.py" \
--exclude="logging_manager.py" | wc -lThe centralized logging system provides convenience functions for backward compatibility:
# All of these work and map to appropriate categories:
from src.utils.logging_manager import (
get_logger, # General-purpose (preferred)
get_backend_logger, # backend category
get_frontend_logger, # frontend category
get_llm_logger, # llm category
get_debug_logger, # debug category
get_audit_logger, # audit category
)
# Usage:
backend_logger = get_backend_logger("my_module")
llm_logger = get_llm_logger("my_llm_code")Solution: Ensure LoggingManager.initialize() is called during app startup
- Already configured in
backend/main.pyandbackend/app_factory.py - For standalone scripts, logging is initialized on first
get_logger()call
Solution: Check log directory permissions and environment variables
# Verify logs directory exists and is writable
mkdir -p logs
chmod 755 logs
# Check environment variables
echo $AUTOBOT_LOG_LEVEL
echo $AUTOBOT_LOGS_DIRSolution: Verify RotatingFileHandler configuration in logging_manager.py
- Default: 10MB max size, 5 backups
- Check disk space and permissions
Solution: Remove any remaining logging.basicConfig() calls
# Find remaining basicConfig calls
grep -r "logging.basicConfig" . --include="*.py" --exclude="logging_manager.py"For each file being migrated:
- Replace
import loggingwithfrom src.utils.logging_manager import get_logger - Replace
logger = logging.getLogger(__name__)withlogger = get_logger(__name__, category) - Remove all
logging.basicConfig()calls - Add comment
# Logging configured via centralized logging_manager - Choose appropriate category (backend, frontend, llm, debug, audit)
- Test that logging works correctly
- Verify no duplicate log entries
- Check log files are created in
logs/directory
- ✅ backend/main.py
- ✅ backend/app_factory_enhanced.py
- ✅ backend/utils/redis_compatibility.py
- ✅ src/project_state_manager.py
- ✅ autobot-backend/agents/research_agent.py
- ✅ autobot-backend/utils/system_context.py
- Scripts: ~20-30 files
- Tests: ~70-80 files
Total Migrated: 6 core files (critical paths complete) Remaining: Non-critical scripts and tests (can be migrated incrementally)
- Centralized Logging Implementation:
autobot-backend/utils/logging_manager.py - GitHub Issue: #42 - Logging Configuration Standardization
- Python Logging Documentation: https://docs.python.org/3/library/logging.html
- RotatingFileHandler: https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler
Last Updated: 2025-01-14 Migration Status: Core production files complete (6/6)