diff options
| author | historia <historiavg@proton.me> | 2026-08-19 03:32:06 -0400 |
|---|---|---|
| committer | historia <historiavg@proton.me> | 2026-08-19 03:32:06 -0400 |
| commit | f1f8e899c46de80d7f9fbb8f0b53983e18167bd2 (patch) | |
| tree | f871476a9b0288bded9d3757d6c39e2061967534 /converter/converter.py | |
| parent | 24cb17a35ebaf5829cefa943c5a528d4accd7dfc (diff) | |
| download | tts-audiobook-generator-f1f8e899c46de80d7f9fbb8f0b53983e18167bd2.tar.gz | |
fix: reduce excessive logging
Diffstat (limited to 'converter/converter.py')
| -rw-r--r-- | converter/converter.py | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/converter/converter.py b/converter/converter.py index 3e5d628..30418e5 100644 --- a/converter/converter.py +++ b/converter/converter.py @@ -19,19 +19,32 @@ from .tts import FasterTTSClient, QwenTTSClient, normalize_language, speaker_dis logger = logging.getLogger(__name__) +def _console_log_filter(record: logging.LogRecord) -> bool: + """Keep httpx/httpcore request logs out of the console (file only).""" + return not record.name.startswith(("httpx", "httpcore")) + + def setup_logging(debug: bool = False) -> None: - """Configure logging to both a dated file and the console.""" + """Configure logging to a dated file and the console. + + The file keeps the full record (DEBUG with --debug), including httpx + request logs. The console handler only surfaces warnings and errors + (DEBUG with --debug) so progress prints are never mirrored as + timestamped log lines; httpx/httpcore request logs stay file-only. + """ config.LOGS_FOLDER.mkdir(parents=True, exist_ok=True) + file_handler = logging.FileHandler( + config.LOGS_FOLDER / f"audiobook_{datetime.now():%Y%m%d}.log", + encoding="utf-8", + ) + file_handler.setLevel(logging.DEBUG if debug else logging.INFO) + console_handler = logging.StreamHandler(sys.stdout) + console_handler.setLevel(logging.DEBUG if debug else logging.WARNING) + console_handler.addFilter(_console_log_filter) logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", - handlers=[ - logging.FileHandler( - config.LOGS_FOLDER / f"audiobook_{datetime.now():%Y%m%d}.log", - encoding="utf-8", - ), - logging.StreamHandler(sys.stdout), - ], + handlers=[file_handler, console_handler], ) if debug: logging.getLogger("converter").setLevel(logging.DEBUG) @@ -323,7 +336,6 @@ class AudiobookConverter: duration = time.time() - start_time logger.info("Conversion completed in %dm %ds: %s", int(duration // 60), int(duration % 60), output_path) - print(f"[SUCCESS] Conversion completed in {int(duration // 60)}m {int(duration % 60)}s") return True def _synthesize_chunks(self, chunks: List[str], @@ -362,20 +374,18 @@ class AudiobookConverter: print(f"[OK] Chunk {chunk_num:3d}/{total_chunks} completed") logger.info("+ Chunk %d/%d completed", chunk_num, total_chunks) else: - print(f"[FAIL] Chunk {chunk_num:3d}/{total_chunks} FAILED") - logger.error("- Chunk %d/%d failed", chunk_num, total_chunks) + logger.error("Chunk %d/%d failed", chunk_num, total_chunks) except Exception as exc: results[chunk_num] = None - print(f"[ERROR] Chunk {chunk_num:3d}/{total_chunks} ERROR: {exc}") - logger.error("- Chunk %d/%d error: %s", chunk_num, total_chunks, exc) + logger.error("Chunk %d/%d error: %s", chunk_num, total_chunks, exc) successful_chunks = sum(1 for path in results.values() if path) print(f"\n{'=' * 50}") print("CHUNK PROCESSING COMPLETE") print(f"Successful: {successful_chunks}/{total_chunks}") print(f"{'=' * 50}") - logger.info("Qwen processing completed: %d/%d chunks", successful_chunks, total_chunks) + logger.info("Chunk processing completed: %d/%d chunks", successful_chunks, total_chunks) return results def _convert_text(self, text: str, output_path: Path, start_time: float, @@ -415,7 +425,8 @@ class AudiobookConverter: chunk_sizes = [len(chunk.split()) for chunk in chunks] avg_chunk_size = sum(chunk_sizes) / len(chunk_sizes) logger.info("Split into %d chunks (avg %.0f words per chunk)", total_chunks, avg_chunk_size) - print(f"[INFO] Processing {total_chunks} chunks via Qwen API...") + backend = "faster TTS API" if self.faster else "Qwen API" + print(f"[INFO] Processing {total_chunks} chunks via {backend}...") results = self._synthesize_chunks(chunks, debug_dir=debug_dir) successful_chunks = sum(1 for path in results.values() if path) @@ -446,7 +457,6 @@ class AudiobookConverter: f"({successful_chunks}/{total_chunks} chunks)") else: logger.info("Conversion completed in %dm %ds: %s", minutes, seconds, output_path) - print(f"[SUCCESS] Conversion completed in {minutes}m {seconds}s") else: logger.error("Failed to combine chunks into final audiobook") |
