Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(cli): silence structlog/stdlib logs on stdout when --format json or md is used (#242)

MohammadYusifa2f54d14fc68
diff --git a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
index e1f90c9c8..890d7f154 100644
--- a/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/dead_code_cmd.py
@@ -12,6 +12,7 @@ from repowise.cli.helpers import (
resolve_command_target,
resolve_repo_path,
run_async,
+ silence_logs_for_machine_output,
)
@@ -86,6 +87,9 @@ def dead_code_command(
--repo <alias> to target a different repo. Cross-repo dead-code
detection is not yet supported — run once per repo for now.
"""
+ if fmt != "table":
+ silence_logs_for_machine_output()
+
from pathlib import Path as PathlibPath
from repowise.core.analysis.dead_code import DeadCodeAnalyzer
diff --git a/packages/cli/src/repowise/cli/commands/health_cmd.py b/packages/cli/src/repowise/cli/commands/health_cmd.py
index 557867fa2..ed9c978eb 100644
--- a/packages/cli/src/repowise/cli/commands/health_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/health_cmd.py
@@ -17,6 +17,7 @@ from repowise.cli.helpers import (
err_console,
resolve_command_target,
run_async,
+ silence_logs_for_machine_output,
)
@@ -111,6 +112,12 @@ def health_command(
from repowise.core.analysis.health.coverage import parse as parse_coverage
from repowise.core.ingestion import ASTParser, FileTraverser, GraphBuilder
+ # Silence structlog/stdlib info+debug lines when the user asked for a
+ # machine-readable format so stdout is pure JSON/Markdown and safe to
+ # pipe into jq or other tools (e.g. `repowise health --format json | jq .kpis`).
+ if fmt != "table":
+ silence_logs_for_machine_output()
+
# Status output goes to stderr when the user asked for a machine-readable
# format — otherwise rich's banner pollutes stdout and breaks
# `repowise health --format json | jq …` (and the CI smoke test).
diff --git a/packages/cli/src/repowise/cli/helpers.py b/packages/cli/src/repowise/cli/helpers.py
index 55c79ca75..95a826a69 100644
--- a/packages/cli/src/repowise/cli/helpers.py
+++ b/packages/cli/src/repowise/cli/helpers.py
@@ -30,6 +30,41 @@ STATE_FILENAME = "state.json"
REPOWISE_DIR = ".repowise"
+# ---------------------------------------------------------------------------
+# Logging / structlog helpers
+# ---------------------------------------------------------------------------
+
+
+def silence_logs_for_machine_output() -> None:
+ """Suppress info/debug log output when stdout is machine-readable (JSON/md).
+
+ Structlog and stdlib loggers write to stdout by default. When a command
+ emits JSON or Markdown, those lines corrupt the output for downstream
+ consumers (e.g. ``repowise health --format json | jq .kpis``).
+
+ Call this at the top of any command that supports ``--format json`` or
+ ``--format md`` before the ingestion pipeline starts.
+ """
+ import logging
+
+ logging.getLogger("httpx").setLevel(logging.ERROR)
+ logging.getLogger("httpcore").setLevel(logging.ERROR)
+ for _name in ("repowise.core", "repowise.server"):
+ logging.getLogger(_name).setLevel(logging.ERROR)
+ try:
+ import structlog
+
+ # cache_logger_on_first_use=False is required: module-level
+ # ``structlog.get_logger`` calls snapshot the logger before configure()
+ # runs and would bypass this filter without it.
+ structlog.configure(
+ wrapper_class=structlog.make_filtering_bound_logger(logging.ERROR),
+ cache_logger_on_first_use=False,
+ )
+ except ImportError:
+ pass
+
+
# ---------------------------------------------------------------------------
# Async bridge
# ---------------------------------------------------------------------------