Complete worked example
python
import io
import json
import logging
def event_line(country, count, api_key):
if not isinstance(country, str) or country not in {"US", "IL", "UA", "CA"} or type(count) is not int or not 0 <= count <= 10000:
raise ValueError("bounded public event fields required")
event = {"country": country, "count": count}
stream = io.StringIO()
logger = logging.Logger("learning.client", level=logging.INFO)
handler = logging.StreamHandler(stream)
logger.addHandler(handler)
try:
logger.info(json.dumps(event, sort_keys=True, separators=(",", ":")))
return stream.getvalue()
finally:
logger.removeHandler(handler)
handler.close()- Reasoning
- Log an allowlisted operational event instead of serializing the entire request or configuration. Compare the stated values and failure boundary before changing the implementation.