Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Develop a Custom Log Store

Learning how to develop a custom log store.

Before diving into the specifics of this component type, it is beneficial to familiarize yourself with our general guide to writing custom component flavors in ZenML. This guide provides an essential understanding of ZenML's component flavor concepts.

ZenML comes equipped with Log Store implementations that you can use to store logs in your artifact store, export to OpenTelemetry-compatible backends, or send to Datadog. However, if you need to use a different logging backend, you can extend ZenML to provide your own custom Log Store implementation.

Base Abstraction

The log store is responsible for collecting, storing, and retrieving logs during pipeline execution. Let's take a deeper dive into the fundamentals behind its abstraction, namely the BaseLogStore class:

  1. Origins: A BaseLogStoreOrigin represents the source of log records (e.g., a step execution). When logging starts, you register an origin with the log store, then emit logs through the log store referencing that origin. When logging ends, you deregister the origin to release resources.

  2. Core methods: The base class defines four abstract methods that must be implemented:

    • emit(): Process and export a log record for a given origin

    • _release_origin(): Called when logging for an origin is complete (cleanup resources)

    • flush(): Ensure all pending logs are exported

    • fetch(): Retrieve one page of stored log entries

  3. Page size: Use resolve_limit() to apply default_query_size and the LOGS_MAX_ENTRIES_PER_REQUEST cap. Override default_query_size for backend defaults and apply any lower provider limit.

  4. Thread safety: The base implementation includes locking mechanisms to ensure thread-safe operation.

Here's a simplified view of the base implementation:

from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, Type
import logging
import threading

from zenml.constants import LOGS_MAX_ENTRIES_PER_REQUEST
from zenml.enums import StackComponentType
from zenml.models import (
    LogsEntriesFilter,
    LogsEntriesResponse,
    LogsResponse,
)
from zenml.stack import Flavor, StackComponent, StackComponentConfig


class BaseLogStoreConfig(StackComponentConfig):
    """Base configuration for all log stores."""


class BaseLogStoreOrigin:
    """Represents the source of log records (e.g., a step execution)."""

    def __init__(
        self,
        name: str,
        log_store: "BaseLogStore",
        log_model: LogsResponse,
        metadata: Dict[str, Any],
    ) -> None:
        self._name = name
        self._log_store = log_store
        self._log_model = log_model
        self._metadata = metadata

    @property
    def name(self) -> str:
        """The name of the origin."""
        return self._name

    @property
    def log_model(self) -> LogsResponse:
        """The log model associated with the origin."""
        return self._log_model

    @property
    def metadata(self) -> Dict[str, Any]:
        """The metadata associated with the origin."""
        return self._metadata

    def deregister(self) -> None:
        """Deregister the origin from the log store."""
        self._log_store.deregister_origin(self)


class BaseLogStore(StackComponent, ABC):
    """Base class for all ZenML log stores."""

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self._origins: Dict[str, BaseLogStoreOrigin] = {}
        self._lock = threading.RLock()

    @property
    def origin_class(self) -> Type[BaseLogStoreOrigin]:
        """Class of the origin used with this log store."""
        return BaseLogStoreOrigin

    def register_origin(
        self, name: str, log_model: LogsResponse, metadata: Dict[str, Any]
    ) -> BaseLogStoreOrigin:
        """Register an origin for a logging context."""
        with self._lock:
            origin = self.origin_class(name, self, log_model, metadata)
            self._origins[name] = origin
            return origin

    def deregister_origin(self, origin: BaseLogStoreOrigin) -> None:
        """Deregister an origin and finalize its logs."""
        with self._lock:
            if origin.name not in self._origins:
                return
            self._release_origin(origin)
            del self._origins[origin.name]
            if len(self._origins) == 0:
                self.flush(blocking=False)

    @abstractmethod
    def emit(
        self,
        origin: BaseLogStoreOrigin,
        record: logging.LogRecord,
        metadata: Optional[Dict[str, Any]] = None,
    ) -> None:
        """Process a log record for the given origin."""

    @abstractmethod
    def _release_origin(self, origin: BaseLogStoreOrigin) -> None:
        """Finalize logging for an origin and release resources."""

    @abstractmethod
    def flush(self, blocking: bool = True) -> None:
        """Flush all pending logs."""

    @property
    def default_query_size(self) -> int:
        """Default page size when no limit is specified."""
        return LOGS_MAX_ENTRIES_PER_REQUEST

    def resolve_limit(self, limit: Optional[int]) -> int:
        """Resolve the page size and apply the global limit."""

    @abstractmethod
    def fetch(
        self,
        logs_model: LogsResponse,
        start: Optional[str] = None,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        filter_: Optional[LogsEntriesFilter] = None,
    ) -> LogsEntriesResponse:
        """Fetch a page of log entries."""


class BaseLogStoreFlavor(Flavor):
    """Base class for all ZenML log store flavors."""

    @property
    def type(self) -> StackComponentType:
        return StackComponentType.LOG_STORE

    @property
    def config_class(self) -> Type[BaseLogStoreConfig]:
        return BaseLogStoreConfig

    @property
    @abstractmethod
    def implementation_class(self) -> Type["BaseLogStore"]:
        """Implementation class for this flavor."""

This is a slimmed-down version of the base implementation. For the full implementation with complete docstrings, check the SDK docs.

Extending the OTEL Log Store

For most custom implementations, you'll want to extend OtelLogStore rather than BaseLogStore directly. The OTEL Log Store provides:

  • OpenTelemetry infrastructure (LoggerProvider, BatchLogRecordProcessor; stdlib bridge via opentelemetry-instrumentation-logging)

  • Automatic log batching and retry logic

  • Standard OTEL log format conversion

To create a custom OTEL-based log store, you only need to implement:

  1. get_exporter(): Return your custom log exporter

  2. fetch(): Retrieve logs from your backend (optional, raise NotImplementedError if not supported)

Creating a Custom Log Exporter

If you're using a custom backend, you'll need to implement a log exporter. The exporter receives batches of OpenTelemetry log records and sends them to your backend:

Implementing Log Fetching

Implement fetch() to retrieve logs from your backend for the ZenML dashboard and API.

Return each page in chronological order. start selects the initial end of the stream (oldest or newest); choose a supported default when omitted and reject unsupported values.

Expose native continuation tokens through before and after, using only the directions the backend supports. Encode each token with its filters, fixed absolute time bounds, direction, and page size so clients can continue with only the cursor. Reject conflicting continuation parameters. Validate the payload and stream IDs, and build backend stream restrictions from the authorized logs_model. Unsigned cursors do not provide authorization or tamper detection.

LogEntry.id is a UUID. Derive a UUID5 from a stable provider event ID and a fixed provider namespace, as shown below. Keep this mapping consistent across deployments and log stores; message text and timestamps do not uniquely identify events.

Raise LogStoreError, LogStoreUnavailableError, or LogStoreRateLimitError for backend failures. Log endpoints map these to HTTP 502, 503, and 429; direct SDK calls raise the exceptions. Set retry_after in seconds to include an HTTP Retry-After header.

The example below assumes native pagination in both directions. Adapt its request fields to your provider.

Build Your Own Custom Log Store

Follow these steps to create and register your custom log store:

  1. Create the implementation: Implement your log store class, configuration, and flavor as shown above.

  2. Create the exporter (if needed): Implement a custom LogRecordExporter for your backend.

  3. Register the flavor: Use the CLI to register your custom flavor:

For example, if your flavor class MyLogStoreFlavor is defined in flavors/my_log_store.py:

  1. Verify registration: Check that your flavor appears in the list:

  1. Register and use your log store:

Important timing notes:

  • The CustomLogStoreFlavor class is imported when registering the flavor via CLI.

  • The CustomLogStoreConfig class is imported when registering/updating a stack component (used for validation).

  • The CustomLogStore class is only imported when the component is actually used.

This separation allows you to register flavors even when their dependencies aren't installed locally.

Best Practices

  1. Extend OtelLogStore: Unless you have specific requirements, extend OtelLogStore to benefit from built-in batching and retry logic.

  2. Handle failures gracefully: Log export failures shouldn't crash your pipeline. Return LogRecordExportResult.FAILURE and log warnings.

  3. Implement retry logic: For network-based backends, implement retry logic in your exporter.

  4. Use secrets for credentials: Store API keys and tokens in ZenML secrets, not in the config directly.

  5. Test thoroughly: Test your implementation with various log volumes and failure scenarios.

  6. Document configuration: Clearly document all configuration options and their defaults.

  7. Keep fetch() simple: Remember that fetch() runs on the server with limited dependencies. Use only built-in Python libraries and HTTP APIs.

  8. Make one backend request per fetch: Return short pages with continuation tokens instead of looping to fill a page. Cap requests at the backend's page-size limit.

ZenML Scarf

Last updated

Was this helpful?