Skip to content

Trace Logging

The Zelos SDK provides a TraceLoggingHandler that integrates Python's standard logging framework with the Zelos trace system. This allows you to automatically capture Python log messages as trace events.

TraceLoggingHandler

A logging handler that sends log records to the Zelos SDK trace system.

import logging
import zelos_sdk
from zelos_sdk.hooks.logging import TraceLoggingHandler

zelos_sdk.init()

# Set the level to info (default is warning)
logging.basicConfig(level=logging.INFO)

# Add the trace logging handler to the root logger
logging.getLogger().addHandler(TraceLoggingHandler())

# Now all log messages will be captured as trace events
logger = logging.getLogger("my_app")
logger.info("Application started")
logger.warning("Low battery: %d%%", 15)
logger.error("Connection failed")

Configuration

The handler accepts optional parameters:

# Custom source name and log level
handler = TraceLoggingHandler(
    source_name="my_application",  # defaults to "logger"
    level=logging.INFO             # defaults to logging.DEBUG
)

logging.getLogger().addHandler(handler)

Captured Fields

Each log record is captured as a trace event with the following fields:

  • level: Log level name (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  • message: The formatted log message
  • name: Logger name
  • file: Source filename
  • line: Line number where the log was called

Example Usage

#!/usr/bin/env python3
import datetime
import logging
import time

from zelos_sdk.hooks.logging import TraceLoggingHandler
import zelos_sdk

zelos_sdk.init()

# Setup both console and trace logging
logging.basicConfig(level=logging.INFO)
logging.getLogger().addHandler(TraceLoggingHandler())

# Log periodically
logger = logging.getLogger("time_service")
while True:
    now = datetime.datetime.now()
    logger.info("Current time: %s", now)
    time.sleep(5)

This automatically captures all log messages as structured trace events that can be viewed in the Zelos App or analyzed programmatically.