We're launching a new cybersecurity venture! Learn more about Snowbit >

Python SDK Python SDK

Last Updated: Mar. 30, 2022

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.


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 


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

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


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

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

# Send message
logger.info("Hello World!")Copy

Parameters and Description

Private KeyThe private key can be found under ‘settings’->’ send your logs’.
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

Advanced Options

You can configure the SDK with dictConfig for Python logging library.
This will allow you to control the structure of the log sent to Coralogix.

import logging


logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'default': { 'format': '[%(asctime)s]: %(levelname)s: %(message)s', } }, 'handlers': { 'coralogix': { 'class': 'coralogix.handlers.CoralogixLogger', 'level': 'DEBUG', 'formatter': 'default', 'private_key': PRIVATE_KEY, 'app_name': APP_NAME, 'subsystem': SUB_SYSTEM, } }, 'root': { 'level': 'DEBUG', 'handlers': [ 'coralogix', ] }, 'loggers': { 'backend': { 'level': 'DEBUG', 'handlers': [ 'coralogix', ] } } })


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:


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 *

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