Files
aitbc/docs/quality/logging-inconsistencies-analysis.md
aitbc 573aae065b
Some checks failed
API Endpoint Tests / test-api-endpoints (push) Successful in 56s
Blockchain Synchronization Verification / sync-verification (push) Failing after 3s
CLI Tests / test-cli (push) Failing after 5s
Coverage Phase 1 (70% Target) / test-coverage-70 (push) Failing after 19s
Coverage Phase 2 (85% Target) / test-coverage-85 (push) Failing after 18s
Cross-Chain Functionality Tests / test-cross-chain-sync (push) Successful in 3s
Cross-Chain Functionality Tests / test-cross-chain-transactions (push) Successful in 4s
Cross-Chain Functionality Tests / test-multi-chain-consensus (push) Successful in 5s
Deploy to Testnet / deploy-testnet (push) Failing after 21s
Documentation Validation / validate-docs (push) Failing after 13s
Documentation Validation / validate-policies-strict (push) Successful in 4s
Integration Tests / test-service-integration (push) Failing after 2s
Multi-Chain Island Architecture Tests / test-multi-chain-island (push) Successful in 4s
Multi-Node Blockchain Health Monitoring / health-check (push) Failing after 14s
Node Failover Simulation / failover-test (push) Successful in 9s
P2P Network Verification / p2p-verification (push) Successful in 5s
Package Tests / Python package - aitbc-agent-sdk (push) Successful in 51s
Package Tests / Python package - aitbc-core (push) Failing after 3s
Package Tests / Python package - aitbc-crypto (push) Successful in 22s
Package Tests / Python package - aitbc-sdk (push) Successful in 16s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 21s
Package Tests / JavaScript package - aitbc-token (push) Failing after 18s
Production Tests / Production Integration Tests (push) Failing after 1m9s
Python Tests / test-python (push) Failing after 3s
Security Scanning / security-scan (push) Failing after 41s
Smart Contract Tests / test-solidity (map[name:aitbc-contracts path:contracts]) (push) Failing after 6s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Failing after 7s
Smart Contract Tests / test-foundry (push) Failing after 20s
Smart Contract Tests / lint-solidity (push) Failing after 4s
Smart Contract Tests / deploy-contracts (push) Failing after 5s
Cross-Chain Functionality Tests / aggregate-results (push) Successful in 2s
Multi-Node Stress Testing / stress-test (push) Successful in 2s
Cross-Node Transaction Testing / transaction-test (push) Successful in 3s
feat: complete codebase remediation with all phases
Phase 1: Security fixes
- Added CORSMiddleware to marketplace-service with specific origins
- Fixed blockchain-node auth to fail closed on JWT errors
- Added security regression tests (test_cors_configuration.py, test_dispute_auth.py)

Phase 2: Repository cleanup
- Removed 51 fix/backup/legacy files
- Deleted marketplace-service-debug directory

Phase 3.1: Python version constraints
- Updated aitbc-crypto and aitbc-sdk with requires-python >=3.13
- Added explicit [tool.poetry].packages declarations

Phase 3.2: Agent service DI architecture
- Created aitbc-agent-core package with protocols and shared service
- Implemented adapters for agent-management and coordinator-api
- Created factory functions for gradual migration
- Added migration comments to existing integration files

Phase 4.1: Auth/utils extraction
- Created auth.py module with JWT validation and security utilities
- Created utils.py module with common helpers

Phase 4.2: Router decomposition
- Decomposed router.py into 10 domain modules (58 endpoints)
- Created route table snapshot for verification
- Preserved router_old.py as reference

Phase 5: App shell classification
- Documented app shell patterns across services

Phase 6: Quality gates
- Verified mypy type checking (75% error reduction)
- Analyzed logging inconsistencies with structlog migration plan
- Removed unused orjson dependency

Documentation:
- Created comprehensive remediation report
- Added architecture documentation for DI pattern
- Added quality analysis documents
2026-05-24 20:21:23 +02:00

192 lines
5.7 KiB
Markdown

# Logging Inconsistencies Analysis
## Current State
The codebase uses multiple logging approaches inconsistently across different modules:
### Logging Patterns in Use
1. **Custom AITBC Logging** (`aitbc.aitbc_logging`)
- Used by: blockchain-event-bridge, CLI (legacy)
- Pattern: `from aitbc.aitbc_logging import get_logger`
- Files: ~10+ files
2. **App-Specific Logging** (agent-management)
- Used by: agent-management app
- Pattern: `from .core.logging import setup_logging, get_logger`
- Files: 2+ files
3. **Stdlib Logging** (`logging`)
- Used by: training_setup, examples, scripts
- Pattern: `import logging`
- Files: ~10+ files
4. **Rich Logging** (`rich.logging`)
- Used by: CLI utils
- Pattern: `from rich.logging import RichHandler`
- Files: 1 file
5. **Structlog** (in dependencies)
- Listed in pyproject.toml: `structlog = ">=25.1.0"`
- Not consistently used across codebase
- Files: 0 active usage found
## Inconsistency Issues
### Problems
1. **No single source of truth**: Different apps use different logging approaches
2. **Configuration fragmentation**: Each logging pattern requires separate configuration
3. **Maintenance burden**: Changes to logging behavior require updates in multiple places
4. **Inconsistent log formats**: Different loggers produce different output formats
5. **Testing complexity**: Mocking different logging patterns requires different approaches
### Impact
- Difficult to enforce consistent logging standards
- Hard to aggregate logs across services
- Inconsistent log levels and formats
- Increased cognitive load for developers
## Standardization Recommendation
### Proposed Approach: Structlog with AITBC Wrapper
**Rationale:**
- `structlog` is already in dependencies (`>=25.1.0`)
- Provides structured logging with JSON output for production
- Supports multiple output formats (console, JSON, file)
- Integrates well with modern observability stacks
- Can wrap stdlib logging for backward compatibility
### Implementation Plan
#### Phase 1: Create Standardized Logging Module
Create `aitbc/aitbc_logging.py` with structlog-based implementation:
```python
"""
Standardized logging for AITBC using structlog.
Provides consistent logging across all services.
"""
import structlog
from typing import Any
import logging
import sys
def setup_logging(
level: int = logging.INFO,
json_output: bool = False,
service_name: str = "aitbc"
) -> None:
"""Configure structlog for the application."""
# Configure structlog processors
processors = [
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
]
if json_output:
processors.append(structlog.processors.JSONRenderer())
else:
processors.append(structlog.dev.ConsoleRenderer())
structlog.configure(
processors=processors,
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
# Configure stdlib logging
logging.basicConfig(
format="%(message)s",
level=level,
stream=sys.stdout,
)
def get_logger(name: str) -> structlog.stdlib.BoundLogger:
"""Get a structured logger for the given module."""
return structlog.get_logger(name)
```
#### Phase 2: Gradual Migration
**Priority Order:**
1. **High Priority**: blockchain-node, coordinator-api (core services)
2. **Medium Priority**: agent-management, marketplace-service
3. **Low Priority**: examples, scripts, stubs
**Migration Pattern:**
```python
# Before
import logging
logger = logging.getLogger(__name__)
# After
from aitbc.aitbc_logging import get_logger
logger = get_logger(__name__)
```
#### Phase 3: Configuration Standardization
**Environment Variables:**
- `AITBC_LOG_LEVEL`: Default log level (INFO, DEBUG, WARNING, ERROR)
- `AITBC_LOG_FORMAT`: Output format (json, console)
- `AITBC_SERVICE_NAME`: Service name for log aggregation
**Example Configuration:**
```python
import os
from aitbc.aitbc_logging import setup_logging
setup_logging(
level=getattr(logging, os.getenv("AITBC_LOG_LEVEL", "INFO")),
json_output=os.getenv("AITBC_LOG_FORMAT", "console") == "json",
service_name=os.getenv("AITBC_SERVICE_NAME", "aitbc"),
)
```
## Migration Status
### Current State
- ✅ structlog in dependencies
- ⏸️ Custom logging modules exist (aitbc_logging, app-specific)
- ⏸️ Inconsistent usage across codebase
- ⏸️ No standardized configuration
### Recommended Next Steps
1. Update `aitbc/aitbc_logging.py` to use structlog
2. Create migration guide for developers
3. Migrate core services (blockchain-node, coordinator-api)
4. Update CI/CD to use standardized logging
5. Remove app-specific logging modules after migration
## Benefits of Standardization
1. **Consistency**: Single logging approach across all services
2. **Observability**: Structured logs for better log aggregation
3. **Flexibility**: Easy to switch between console and JSON output
4. **Performance**: Structlog is optimized for production use
5. **Maintainability**: Single module to maintain and update
## Risk Mitigation
1. **Backward Compatibility**: Keep existing logging during migration
2. **Gradual Rollout**: Migrate one service at a time
3. **Testing**: Verify log output after each migration
4. **Rollback Plan**: Can revert to old logging if issues arise
5. **Documentation**: Clear migration guide for developers
## Success Criteria
- [ ] All services use standardized logging
- [ ] Consistent log format across codebase
- [ ] Structlog configuration documented
- [ ] Migration guide created
- [ ] CI/CD uses standardized logging
- [ ] App-specific logging modules removed