60 MINS
17. Operator Toolkit
Module 17: Tooling
Debugging, Logging, Testing, and Profiling Automation Systems
Production automation systems break in ways that development environments never reveal. A scraper that works perfectly for 100 URLs starts failing silently at 10,000. A bot that handles normal messages breaks on edge case inputs. A pipeline that runs fine on your laptop crashes in Docker. This module covers the professional tooling practices that turn debugging from frustration into methodology.
Structured Logging with Loguru
Text
from loguru import logger
import sys
from pathlib import Path
from functools import wraps
import time
import uuid
# Configure loguru for production
logger.remove() # remove default handler
# Console handler — human readable
logger.add(
sys.stdout,
format='<green>{time:HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{line}</cyan> — {message}',
level=DEBUG,
colorize=True
)
# File handler — structured for analysis
logger.add(
logs/automation_{time:YYYY-MM-DD}.log,
format='{time:YYYY-MM-DD HH:mm:ss.SSS} | {level} | {name}:{function}:{line} | {message}',
rotation=00:00, # rotate at midnight
retention=30 days,
compression=gz, # compress old logs
level=INFO
)
# Error-only handler for alerting
logger.add(
logs/errors.log,
level=ERROR,
rotation=10 MB
)
def with_logging(operation: str):
'''Decorator that logs entry, exit, duration, and any exceptions'''
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
correlation_id = str(uuid.uuid4())[:8]
log = logger.bind(operation=operation, correlation_id=correlation_id)
log.info(fStarting {operation})
start = time.time()
try:
result = func(*args, **kwargs)
duration = time.time() - start
log.success(fCompleted {operation} in {duration:.3f}s)
return result
except Exception as e:
duration = time.time() - start
log.exception(f'Failed {operation} after {duration:.3f}s: {type(e).__name__}: {e}')
raise
return wrapper
return decorator
@with_logging(scrape_product_page)
def scrape_product(url: str) -> dict:
# scraping logic
return {}
Testing Automation Code
Text
import pytest
from pathlib import Path
from unittest.mock import Mock, patch, MagicMock
from bs4 import BeautifulSoup
# Test HTML fixtures
SAMPLE_PRODUCT_HTML = '''
<div class="product-card">
<h2 class="product-title">Test Product Name</h2>
<span class="price">$29.99</span>
<span class="availability in-stock">In Stock</span>
<a href="/products/test-product" class="product-link">View</a>
Profiling and Performance
Text
import cProfile
import pstats
import io
from memory_profiler import profile as memory_profile
import time
from functools import wraps
def profile_function(func):
@wraps(func)
def wrapper(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
result = func(*args, **kwargs)
pr.disable()
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats(cumulative)
ps.print_stats(20) # top 20 functions
print(s.getvalue())
return result
return wrapper
# Memory profiling for large scrapers
@memory_profile
def scrape_large_dataset(urls: list):
results = []
for url in urls:
# Each iteration's memory usage is tracked
data = {}
results.append(data)
return results # notice if results grows unboundedly
# Timing decorator for specific operations
def timeit(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
result = func(*args, **kwargs)
elapsed = time.perf_counter() - start
logger.debug(f'{func.__name__} took {elapsed*1000:.2f}ms')
return result
return wrapper
Common Production Failure Modes and Fixes
- Memory Leak: Accumulating results in a list without writing to disk. Fix: batch write every N records and clear the list.
- Connection Pool Exhaustion: Creating new requests.Session() per request instead of sharing one session. Fix: use a single shared session per worker.
- Silent Data Loss: Using bare except clauses that catch exceptions and do nothing. Fix: always log exceptions with full traceback.
- Infinite Wait: Network request with no timeout. Fix: always set timeout= on every requests call and playwright page.goto call.
- Stale Session Cookies: Loading saved cookies from last week. Fix: validate cookies on load and refresh if expired.
- Race Condition: Two workers processing the same URL. Fix: use atomic Redis operations (zpopmax) for task distribution.
Knowledge Check
Ready to test your understanding of 17. Operator Toolkit?