Show table of contents Hide table of contents

Logging from Python

Python applications can log to Seq through the Python logging API with seqlog (recommended), with pygelf and GELF, or via direct HTTP and JSON.

seqlog

The seqlog package implements a Python logging adapter for Seq:

# Indexer-style logging.info("Hello, %s!", "World") # Message template style logging.info("Hello, {name}!", name="World") 

The detailed project documentation includes full configuration and usage examples.

The project is independently developed on GitHub.

GELF

pygelf is a GELF logging handler for Python.

1. Enable the Seq GELF input

See the instructions in Using GELF.

2. Install pygelf

$ pip install pygelf 

3. Configure theGelfUdpHandler

from pygelf import GelfUdpHandler import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger() logger.addHandler(GelfUdpHandler(host='127.0.0.1', port=12201)) 

4. Log some events!

logger.info('Hello, Seq+GELF!') 

OpenTelemetry Python SDK

The OpenTelemetry Python SDK can send logs and traces to Seq. See the opentelemetry-python-sdk-to-seq example for a working example of logging and tracing from Python to Seq.

1. Install the dependencies

pip install opentelemetry-api pip install opentelemetry-sdk pip install opentelemetry-exporter-otlp 

2. Configure the SDK

Set the OTLPLogExporter endpoint to the correct OTLP endpoint.

import logging from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter from opentelemetry._logs import set_logger_provider from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler from opentelemetry.sdk._logs.export import BatchLogRecordProcessor from opentelemetry.sdk.resources import SERVICE_NAME, Resource # Service name is required for most backends resource = Resource(attributes={ SERVICE_NAME: "example service name" }) # configure logging logger_provider = LoggerProvider(resource=resource) set_logger_provider(logger_provider) logger_provider.add_log_record_processor(BatchLogRecordProcessor(OTLPLogExporter(endpoint="http://localhost:5341/ingest/otlp/v1/logs"))) handler = LoggingHandler(level=logging.NOTSET, logger_provider=logger_provider) logging.getLogger().addHandler(handler) logger = logging.getLogger(__name__) 

3. Log some events!

logger.warning("The weather forecast is %s", "Overcast, 24°C") logger_provider.shutdown()