"""Demo: how to use DebugLogger in real scripts. Run this from the cq2_sdk directory: uv run python scripts/demo_debug_log.py It exercises 4 patterns: 1. basic context-manager + step() 2. HTTP request/response correlation 3. error event from a caught exception 4. auto-error on context-manager exit when an exception escapes Output: a new .jsonl file in D:\\传奇\\debug_logs\\. After running, try: cat "D:/传奇/debug_logs/session_*.jsonl" | head # or in OpenCode: ask it to read the newest session_*.jsonl """ from __future__ import annotations import sys from pathlib import Path # allow running this script from anywhere sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src")) from cq2_sdk.debug_log import DebugLogger # noqa: E402 def main() -> int: print("=== Demo 1: basic step logging ===") with DebugLogger(extra={"demo": "v1", "purpose": "pattern_showcase"}) as log: log.step("login_attempt", account="longwu988", server="玖扬复古176") log.info("entered login flow") print("=== Demo 2: HTTP correlation ===") req_id = log.http_request( "POST", "https://api.jiyuhz.cn/auth/v3/login/safe/byPwd", headers={"X-Box-Auth": "", "User-Agent": "996Box/5.1.1"}, body='{"loginInfo":"","channel_id":"abc"}', step="login_attempt", ) # pretend we got a response log.http_response( req_id, status=200, body='{"code":101000,"msg":"需要进行行为校验"}', duration_ms=769.4, step="login_attempt", ) log.info("server demanded geetest, switching solver", req_id=req_id) print("=== Demo 3: caught error ===") try: do_failing_thing() except ValueError as e: log.error(e, step="geetest_solve", reason="bad_validate") print(f"\nlog written to: {log.path}") print("last 5 lines:") lines = log.path.read_text(encoding="utf-8").splitlines() for ln in lines[-5:]: print(f" {ln}") return 0 def do_failing_thing() -> None: raise ValueError("geetest validate token expired") if __name__ == "__main__": sys.exit(main())