Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Service Logger

From Leeroopedia
Attribute Value
Sources litellm/_service_logger.py
Domains Observability, Monitoring, Prometheus, Datadog, OpenTelemetry
last_updated 2026-02-15 16:00 GMT

Overview

The Service Logger provides health monitoring for LiteLLM-adjacent infrastructure services (Redis, PostgreSQL, LiteLLM itself) by dispatching success and failure events to configured observability backends.

Description

ServiceLogging extends CustomLogger and acts as a specialized callback class focused on infrastructure health rather than LLM call observability. It tracks call duration and success/failure status for services defined by the ServiceTypes enum and dispatches telemetry to backends registered in litellm.service_callback.

Supported backends:

  • prometheus_system -- Forwards payloads to PrometheusServicesLogger for Prometheus metric emission.
  • datadog -- Forwards to DataDogLogger for Datadog metric emission.
  • otel -- Forwards to OpenTelemetry logger with parent span propagation.

The class provides both synchronous and asynchronous hooks:

  • service_success_hook() -- Synchronous entry point that detects whether an event loop is running and delegates appropriately (creates a task if running, uses run_until_complete otherwise).
  • async_service_success_hook() / async_service_failure_hook() -- Core async implementations that iterate over configured callbacks.
  • async_log_success_event() -- Hooks into the standard CustomLogger interface to track LiteLLM proxy LLM API call latency.

Backend loggers are lazily initialized via init_*_if_none() helper methods to avoid import-time overhead.

Usage

Instantiate ServiceLogging and register it where infrastructure calls are made (typically in the proxy server startup):

from litellm._service_logger import ServiceLogging

Code Reference

Source Location

/litellm/_service_logger.py (321 lines)

Class: ServiceLogging

Method Signature Purpose
__init__ def __init__(self, mock_testing: bool = False) -> None Initializes service logger; sets up Prometheus if configured
service_success_hook def service_success_hook(self, service, duration, call_type, parent_otel_span=None, start_time=None, end_time=None) Sync entry point for success events
service_failure_hook def service_failure_hook(self, service, duration, error, call_type) Sync entry point for failure events (placeholder)
async_service_success_hook async def async_service_success_hook(self, service, call_type, duration, ...) Async success handler dispatching to backends
async_service_failure_hook async def async_service_failure_hook(self, service, duration, error, call_type, ...) Async failure handler dispatching to backends
async_log_success_event async def async_log_success_event(self, kwargs, response_obj, start_time, end_time) Tracks LiteLLM API call latency as a service metric

Import

from litellm._service_logger import ServiceLogging

I/O Contract

Inputs

Parameter Type Description
service ServiceTypes The service being monitored (e.g., ServiceTypes.REDIS)
duration float Duration of the service call in seconds
call_type str Type of call being made (e.g., "async_get_cache")
error Union[str, Exception] Error that occurred (for failure hooks)
parent_otel_span Optional[Span] Parent OpenTelemetry span for trace propagation

Outputs

The methods dispatch telemetry to configured backends and do not return meaningful values. The ServiceLoggerPayload dataclass is constructed internally and passed to each backend.

Usage Examples

from litellm._service_logger import ServiceLogging
from litellm.types.services import ServiceTypes

service_logger = ServiceLogging()

# Log a successful Redis call
service_logger.service_success_hook(
    service=ServiceTypes.REDIS,
    duration=0.005,
    call_type="async_get_cache",
)

# Log a failed Redis call (async)
await service_logger.async_service_failure_hook(
    service=ServiceTypes.REDIS,
    duration=0.100,
    error=ConnectionError("Redis connection refused"),
    call_type="async_set_cache",
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment