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.

clew/buildlog.py:99

Show on the map →

Called by _run_pipeline

Calls log_path

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
            pass

1 more lines, not shown

Activity · 17 steps
lines 111–111path = log_path(output)lines 112–112handler: logging.Handler | None = Nonelines 113–113trylines 114–114path.parent.mkdir(parents=True, exist_ok=True)lines 115–115handler = _FlushingFileHandler(str(path), mode="w", encoding="utf-8")lines 116–116handler.setFormatter(logging.Formatter(_FORMAT))lines 117–117handler.setLevel(logging.DEBUG)lines 118–118logger.addHandler(handler)lines 119–119except OSError as exclines 121–121logger.debug("build log: cannot write %s (%s) — continuing without it", path, exc)lines 122–122yield Nonelines 123–123returnlines 134–134previous = logger.levellines 135–135◇ if previous == logging.NOTSET or previous > logging.INFOlines 136–136logger.setLevel(logging.INFO)lines 137–137trylines 138–138yield path

Requirements

REQ-DDB-INDEX-002