| 1 | """Shared logging utilities for last30days skill.""" |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | |
| 6 | |
| 7 | def is_debug() -> bool: |
| 8 | val = os.environ.get("LAST30DAYS_DEBUG", "") |
| 9 | return val.lower() in ("1", "true", "yes", "on") |
| 10 | |
| 11 | |
| 12 | def debug(msg: str) -> None: |
| 13 | """Log debug message to stderr (only when LAST30DAYS_DEBUG is set).""" |
| 14 | if is_debug(): |
| 15 | sys.stderr.write(f"[DEBUG] {msg}\n") |
| 16 | sys.stderr.flush() |
| 17 | |
| 18 | |
| 19 | def source_log(prefix: str, msg: str, *, tty_only: bool = True) -> None: |
| 20 | """Log a source module message to stderr. |
| 21 | |
| 22 | Args: |
| 23 | prefix: Source label (e.g. "Reddit", "Bird"). |
| 24 | msg: Message text. |
| 25 | tty_only: If True, only log when stderr is a TTY (avoids cluttering |
| 26 | non-interactive output like Claude Code). |
| 27 | |
| 28 | CONVENTION: source modules under `lib/` must call this with |
| 29 | `tty_only=False`. The default exists to keep ad-hoc callers quiet, but |
| 30 | a source module's logs are observability — silently dropping them under |
| 31 | Claude Code, Codex, or CI hides both failures and success signals from |
| 32 | the user and the synthesis LLM. The convention is enforced by |
| 33 | `tests/test_source_log_visibility.py`. |
| 34 | """ |
| 35 | if tty_only and not sys.stderr.isatty(): |
| 36 | return |
| 37 | sys.stderr.write(f"[{prefix}] {msg}\n") |
| 38 | sys.stderr.flush() |
| 39 |