Skip to content

Python SDK

Coralogix provides a Python SDK that enables you to send logs directly from your application. The SDK leverages Python’s standard logging library and operates asynchronously by using a dedicated thread to manage an internal buffer, which is then flushed to Coralogix.

The full SDK implementation is available in the official Coralogix source code repository.

For a practical reference, see the example project.

Installation

Using pip package manager:

pip install coralogix_logger

Directly from the source code:

git clone https://github.com/coralogix/python-coralogix-sdk.git &&
cd sdk-python &&
python setup.py install 

Implementation

import logging

from coralogix.coralogix_logger import CoralogixLogger # For version 1.x.

from coralogix.handlers import CoralogixLogger # For version 2.x and above. 

PRIVATE_KEY = "[YOUR_PRIVATE_KEY_HERE]"
APP_NAME = "[YOUR_APPLICATION_NAME]"
SUB_SYSTEM = "[YOUR_SUBSYTEM_NAME]"
REGION = "[YOUR_REGION]"  # 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.
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 message
logger.info("Hello World!")
# Send dictionary message
my_dict = {"host": "https://xyz.test.com","log":"Hello to coralogix"}   
logger.info(json.dumps(my_dict))
# Manually flush logger - ensure logs are sent
CoralogixLogger.flush_messages()

Parameters and Description

ParameterDescriptionRequired
Private KeyYour Coralogix Send-Your-Data API keyYes
Application NameThe application Tag you wish to append to every log line sentYes
SubSystem NameThe subsystem Tag you wish to append to every log line sentYes
RegionThe Coralogix region associated with your domainYes (v2.1.0+)

Region Configuration

As of SDK version 2.1.0, it is required to specify your Coralogix region. This ensures your logs are sent to the correct regional endpoint.

You can specify the region in two ways:

Option 1: As a parameter

coralogix_handler = CoralogixLogger(
    PRIVATE_KEY, 
    APP_NAME, 
    SUB_SYSTEM, 
    region="EU1"
)

Option 2: As an environment variable

export CORALOGIX_REGION=EU1

Then initialize without the region parameter:

coralogix_handler = CoralogixLogger(PRIVATE_KEY, APP_NAME, SUB_SYSTEM)

URL method (for backward compatibility)

If you need to use a custom URL or are using an older SDK version, you can set the CORALOGIX_LOG_URL environment variable:

export CORALOGIX_LOG_URL='https://ingress./api/v1/logs'

Select the Coralogix Logs endpoint URL associated with your Coralogix domain here.

Advanced Options

You can configure the format of the specific coralogix handler.

This will allow you to control the log structure sent to Coralogix.

...

# Get a new instance of Coralogix logger.
coralogix_handler = CoralogixLogger(PRIVATE_KEY, APP_NAME, SUB_SYSTEM, region=REGION)

# Create and set new formatter.
my_format = logging.Formatter('{"timestmap":"%(asctime)s","logger":"%(name)s","level":"%(levelname)s","payload":%(message)s}')
coralogix_handler.setFormatter(my_format)

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

...

uWSGI

By default, uWSGI does not enable threading support within the Python interpreter core. This means it is not possible to create background threads from Python code. As the Coralogix logger relies on being able to create a background thread (for sending logs), this option is required.

You can enable threading either by passing –enable-threads to uWSGI command line:

uwsgi wsgi.ini --enable-threads

Another option is to enable threads in your wsgi.ini file:

wsgi.ini

enable-threads = true

If you are using multiple processes/workers and you don’t use "lazy-apps = true" then you must wait for the process to finish the fork before you can send logs with Coralogix logger

You can configure the logger during the initialization process but you must wait for the fork to complete before you can actually send your logs.

You can use uWSGI @postfork decorator to be sure when it’s safe to use Coralogix logger:

import uwsgi
from uwsgidecorators import *

@postfork
def on_worker_ready():
    #It is now safe to send logs with Coralogix logger

Need help? We love to assist our customers, simply reach out via our in-app chat, and we will walk you through, step by step.

Was this helpful?