Skip to content

sink

sink #

Sink — where a log goes: one method that sees every entry as it is made or changed.

Sink #

Bases: ABC

A destination for a log; subclass and implement emit.

emit is called under the log's lock for every entry, once when it is made (revision == 0) and once per change after that, in order; keep it quick. A sink that keeps its own tree upserts by entry.id; one that prints decides from revision and status. start and finish bracket the to() block for sinks that need the whole Log or a last flush.

class Collect(cg.logging.Sink):
    def __init__(self) -> None:
        self.seen: list[str] = []

    def emit(self, entry: cg.logging.Entry) -> None:
        self.seen.append(entry.message)
Source code in capturegraph-lib/capturegraph/logging/sink.py
class Sink(ABC):
    """A destination for a log; subclass and implement ``emit``.

    ``emit`` is called under the log's lock for every entry, once when it is
    made (``revision == 0``) and once per change after that, in order; keep it
    quick. A sink that keeps its own tree upserts by ``entry.id``; one that
    prints decides from ``revision`` and ``status``. ``start`` and ``finish``
    bracket the ``to()`` block for sinks that need the whole ``Log`` or a
    last flush.

    ```python
    class Collect(cg.logging.Sink):
        def __init__(self) -> None:
            self.seen: list[str] = []

        def emit(self, entry: cg.logging.Entry) -> None:
            self.seen.append(entry.message)
    ```
    """

    def start(self, log: Log) -> None:
        """``log`` is about to be written; keep it if a snapshot is ever needed."""
        return

    @abstractmethod
    def emit(self, entry: Entry) -> None:
        """``entry`` was just made or just changed."""

    def finish(self) -> None:
        """The ``to()`` block has ended; flush whatever is pending."""
        return

emit(entry) abstractmethod #

entry was just made or just changed.

Source code in capturegraph-lib/capturegraph/logging/sink.py
@abstractmethod
def emit(self, entry: Entry) -> None:
    """``entry`` was just made or just changed."""

finish() #

The to() block has ended; flush whatever is pending.

Source code in capturegraph-lib/capturegraph/logging/sink.py
def finish(self) -> None:
    """The ``to()`` block has ended; flush whatever is pending."""
    return

start(log) #

log is about to be written; keep it if a snapshot is ever needed.

Source code in capturegraph-lib/capturegraph/logging/sink.py
def start(self, log: Log) -> None:
    """``log`` is about to be written; keep it if a snapshot is ever needed."""
    return