Learn more about Streama© – the foundational technology behind our stateful streaming data platform. Learn More

Python SDK

This document includes cluster dependent URL’s. Each URL has a variable part (in Italic). Please match this part with a row entry within the following table. Copy the table row entry located under the column that matches the top level domain of your Coralogix account (.com, .in etc.). Replace the variable part of the URL with this entry.

SSL Certificateshttps://coralogix-public.s3-eu-west-1.amazonaws.com/certificate/Coralogix-EU.crthttps://www.amazontrust.com/repository/AmazonRootCA1.pemhttps://coralogix-public.s3-eu-west-1.amazonaws.com/certificate/Coralogix-IN


$ pip install coralogix_logger

Or directly from the sources:

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


Using Coralogix SDK requires four mandatory parameters and one optional parameter:

private_key (String): A unique ID that represents your company. The private key can be found under ‘settings’->’ send your logs’. It is located in the upper left corner.

application Name(String): The name of your main application. For example, a company named Startup which develops app_1 and app_2 can use “Startup app_1” and “Startup app_2” for this parameter; or if they want to debug their test environment they might insert the “Startup app_1 – Test” or “Startup app_1 – Staging”.

subsystem Name(String): The name of your sub-system. Your application probably has multiple subsystems, e.g. “Backend servers”, “Middleware”, “Frontend servers”, “Database servers” etc. In order to help you examine only the data you need, inserting the subsystem parameter is vital.

If your Coralogix account ttop level domain is different than ‘.com’ add the following environment variable, CORALOGIX_LOG_URL=https://api.Cluster URL/api/v1/logs


Adding Coralogix logging handler in your logging system:

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


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

Also, you can configure the SDK with dictConfig for Python logging library:

import logging PRIVATE_KEY = '[YOUR_PRIVATE_KEY_HERE]' APP_NAME = '[YOUR_APPLICATION_NAME]' SUB_SYSTEM = '[YOUR_SUBSYSTEM_NAME]' 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 book your implementation session, and we will walk you through, step by step.