Configure loguru with JSON output to log_file and intercept stdlib logging. Idempotent: skips if already configured (e.g. hot reload). Use force=True to reconfigure (e.g. in tests with a different log path). When ``verbose_third_party`` is false, noisy HTTP and Telegram loggers are cap
(
log_file: str | Path, *, force: bool = False, verbose_third_party: bool = False
)
| 105 | |
| 106 | |
| 107 | def configure_logging( |
| 108 | log_file: str | Path, *, force: bool = False, verbose_third_party: bool = False |
| 109 | ) -> None: |
| 110 | """Configure loguru with JSON output to log_file and intercept stdlib logging. |
| 111 | |
| 112 | Idempotent: skips if already configured (e.g. hot reload). |
| 113 | Use force=True to reconfigure (e.g. in tests with a different log path). |
| 114 | |
| 115 | When ``verbose_third_party`` is false, noisy HTTP and Telegram loggers are capped |
| 116 | at WARNING unless explicitly configured otherwise. |
| 117 | """ |
| 118 | global _configured |
| 119 | if _configured and not force: |
| 120 | return |
| 121 | _configured = True |
| 122 | |
| 123 | # Remove default loguru handler (writes to stderr) |
| 124 | logger.remove() |
| 125 | |
| 126 | log_path = Path(log_file) |
| 127 | log_path.parent.mkdir(parents=True, exist_ok=True) |
| 128 | |
| 129 | # Truncate log file on fresh start for clean debugging |
| 130 | log_path.write_text("") |
| 131 | |
| 132 | # Add file sink: JSON lines, DEBUG level, context vars at top level |
| 133 | logger.add( |
| 134 | log_file, |
| 135 | level="DEBUG", |
| 136 | format=_serialize_with_context, |
| 137 | encoding="utf-8", |
| 138 | mode="a", |
| 139 | rotation="50 MB", |
| 140 | enqueue=True, |
| 141 | ) |
| 142 | |
| 143 | # Intercept stdlib logging: route all root logger output to loguru |
| 144 | intercept = InterceptHandler() |
| 145 | logging.root.handlers = [intercept] |
| 146 | logging.root.setLevel(logging.DEBUG) |
| 147 | |
| 148 | third_party = ( |
| 149 | "httpx", |
| 150 | "httpcore", |
| 151 | "httpcore.http11", |
| 152 | "httpcore.connection", |
| 153 | "telegram", |
| 154 | "telegram.ext", |
| 155 | ) |
| 156 | for name in third_party: |
| 157 | logging.getLogger(name).setLevel( |
| 158 | logging.WARNING if not verbose_third_party else logging.NOTSET |
| 159 | ) |