This specification defines the logging system for Pearl-compliant AI agents, ensuring full compatibility with the OLAS network and Pearl platform requirements.
- Mandatory: Agent must produce
log.txtfile in its working directory - Working directory is set by Pearl platform during agent execution
- File must be accessible for Pearl monitoring and debugging
Required Format: [YYYY-MM-DD HH:MM:SS,mmm] [LOG_LEVEL] [agent] Your message
Example:
[2024-03-14 10:30:00,123] [INFO] [agent] Starting command execution
[2024-03-14 10:30:01,456] [DEBUG] [agent] Command parameters: {...}
[2024-03-14 10:30:02,789] [ERROR] [agent] Failed to connect to service
ERROR: Critical errors preventing agent executionWARN: Warning messages about potential issuesINFO: General operational informationDEBUG: Detailed debugging informationTRACE: Verbose execution traces
# Python standard library (no installation required)
import logging
import logging.handlers
from datetime import datetime
import re
import osimport logging
from datetime import datetime
def setup_pearl_logger():
"""Configure logging for Pearl compliance."""
logger = logging.getLogger('agent')
logger.setLevel(logging.INFO)
# Create file handler for log.txt
handler = logging.FileHandler('log.txt', mode='a')
# Pearl-compliant formatter
formatter = PearlFormatter()
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
class PearlFormatter(logging.Formatter):
"""Custom formatter for Pearl compliance."""
def format(self, record):
timestamp = datetime.fromtimestamp(record.created).strftime('%Y-%m-%d %H:%M:%S,%f')[:-3]
return f"[{timestamp}] [{record.levelname}] [agent] {record.getMessage()}"logger = setup_pearl_logger()
# Standard logging calls
logger.info("Agent initialized successfully")
logger.debug("Processing transaction: %s", tx_hash)
logger.error("Connection failed: %s", error_msg)
logger.warn("Retrying operation after timeout")The agent must respect Pearl-provided environment variables:
STORE_PATH: Persistent data storage location
-
Agent Lifecycle
- Initialization and startup
- Shutdown and cleanup
- State recovery after SIGKILL
-
Transaction Operations
- Safe contract interactions
- EOA transactions
- Gas estimation and execution
-
Data Persistence
- State saves to STORE_PATH
- Recovery operations
- Data validation checks
import re
def validate_log_format(log_line):
"""Validate log line matches Pearl format."""
pattern = r'^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\] \[(ERROR|WARN|INFO|DEBUG|TRACE)\] \[agent\] .*'
return bool(re.match(pattern, log_line))def ensure_log_file_exists():
"""Ensure log.txt exists and is writable."""
try:
with open('log.txt', 'a') as f:
f.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S,%f')[:-3]}] [INFO] [agent] Log file initialized\n")
return True
except Exception as e:
print(f"Failed to initialize log file: {e}")
return False- Log Early and Often: Log all significant agent operations
- Use Appropriate Levels: Reserve ERROR for actual failures
- Include Context: Add transaction hashes, addresses, amounts
- Handle Failures: Ensure logging doesn't crash the agent
- Performance: Avoid excessive DEBUG logging in production
This specification ensures full compliance with Pearl platform requirements while providing comprehensive observability for autonomous AI agent operations.