buildlog.py¶
clew/buildlog.py
A per-build log written beside the index, incrementally.
build_log
Iterator[Path | None] clew.buildlog.build_log(Path output)
Tee this build's log records to a file beside the index.
Source · 47 lines
def build_log(output: Path) -> Iterator[Path | None]:
"""YIELDS None RATHER THAN RAISING when the log cannot be opened. The caller is a build, and
a build must not fail because a diagnostic file could not be created — that would convert an
observability feature into a new failure mode, which is the opposite of the point.
TEE, NOT REDIRECT. The handler is ADDED to the existing logger, so console output and the
MCP server's in-memory capture are both untouched and see exactly what they saw before.
@brief Attach a file handler for the duration of one build.
@return Yields the log path, or None.
@version 1
"""
path = log_path(output)
handler: logging.Handler | None = None
try:
path.parent.mkdir(parents=True, exist_ok=True)
handler = _FlushingFileHandler(str(path), mode="w", encoding="utf-8")
handler.setFormatter(logging.Formatter(_FORMAT))
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
except OSError as exc:
## An unwritable state directory is a real deployment. Say so once, at DEBUG, and build.
logger.debug("build log: cannot write %s (%s) — continuing without it", path, exc)
yield None
return
## THE LOGGER'S LEVEL, NOT JUST THE HANDLER'S — WITHOUT THIS THE FILE IS NEARLY EMPTY.
## A handler set to DEBUG never sees a record the LOGGER filtered out first, and the `clew`
## logger is lowered to INFO only by `_configure_logging`, which the CLI calls and the
## library and MCP entry points do not. So the build log captured warnings and nothing else
## on exactly the paths it exists for. MEASURED before this line: a log holding one config
## warning and none of the stage progress. `capture_pipeline_output` raises the level for
## the same reason.
##
## RAISED ONLY WHEN IT WOULD OTHERWISE HIDE INFO, so `--verbose` (DEBUG) is not quietly
## downgraded by attaching a log.
previous = logger.level
if previous == logging.NOTSET or previous > logging.INFO:
logger.setLevel(logging.INFO)
try:
yield path
finally:
logger.setLevel(previous)
logger.removeHandler(handler)
try:
handler.close()
except OSError: # pragma: no cover - already gone
pass1 more lines, not shown