Coralogix Python SDK

2.1.1 · active · verified Sun Apr 12

The Coralogix Python SDK enables sending logs directly from Python applications to the Coralogix platform. It leverages Python's standard logging library and operates asynchronously using a dedicated thread for log buffering and flushing. The current version is 2.1.1, and the library is actively maintained with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Coralogix logger and send basic log messages. It configures a standard Python logger with the Coralogix handler, setting the necessary API key, application name, subsystem, and region. Environment variables are used for sensitive credentials. Logs are explicitly flushed to ensure delivery.

import logging
import os
import json
from coralogix.handlers import CoralogixLogger

PRIVATE_KEY = os.environ.get('CORALOGIX_PRIVATE_KEY', 'YOUR_PRIVATE_KEY')
APP_NAME = os.environ.get('CORALOGIX_APP_NAME', 'my-python-app')
SUB_SYSTEM = os.environ.get('CORALOGIX_SUB_SYSTEM', 'my-subsystem')
REGION = os.environ.get('CORALOGIX_REGION', 'EU1') # e.g., 'EU1', 'US1', 'AP1'

# Get an instance of Python standard logger.
logger = logging.getLogger("Python Logger")
logger.setLevel(logging.DEBUG)

# Get a new instance of Coralogix logger.
# Region is required as of SDK version 2.1.0
coralogix_handler = CoralogixLogger(PRIVATE_KEY, APP_NAME, SUB_SYSTEM, region=REGION)

# Add coralogix logger as a handler to the standard Python logger.
logger.addHandler(coralogix_handler)

# Send messages
logger.info("Hello World from Coralogix Python SDK!")
my_dict = {"host": "localhost", "message": "This is a structured log."}
logger.info(json.dumps(my_dict))

# Manually flush logger to ensure logs are sent before application exit
CoralogixLogger.flush_messages()
print("Logs sent to Coralogix.")

view raw JSON →