Our next-gen architecture is built to help you make sense of your ever-growing data. Watch a 4-min demo video!

Back to All Integrations

Python SDK Python SDK

Last Updated: Nov. 24, 2023

Coralogix provides an SDK to allow you to send logs directly from your Python application.
This SDK is based on the generic logging library in Python.
The SDK is working in Async mode, opening a thread to maintain a buffer which is later pushed into Coralogix.

Here is a link to the source code.

Here is an 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]"

# 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)

# 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

ParameterDescription
Private KeyYour Coralogix Send-Your-Data API key
Application NameThe application Tag you wish to append to every log line sent
SubSystem NameThe subsystem Tag you wish to append to every log line sent

Coralogix supports multiple geo regions depending on where your account is located. This part is required only if you are not in the EU region The URL can be provided as an Environment variable.

export CORALOGIX_LOG_URL = 'https://<coralogix_cluster_url>:443/api/v1/logs'
Cluster locationcoralogix_cluster_url
USapi.coralogix.us
Indiaapi.app.coralogix.in
Singaporeapi.coralogixsg.com
EUapi.coralogix.com
EU2api.eu2.coralogix.com
CX498api.cx498-aws-us-west-2.coralogix.com

Advanced Options

You can configure the format of the specific coralogix handler.

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

...

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

# 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.

On this page