Coralogix Python integration

Install


$ pip install coralogix_logger

Instructions

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

company_id (Integer): A unique ID which represents your company. This ID will be sent to your mail once you register to Coralogix.

private_key (String): The private key is the password for your company, used to validate your authenticity. This key will be sent to your mail once you register to Coralogix.

application (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 (String): The name of your sub-system. Your application probably has multiple sub-systems, 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.

computer_name (String): Optional. When several servers run the same application/subsystem, you will enjoy the full power of Coralogix if you use a different computer_name per server.

Python implementation example:

import logging
from coralogix.coralogix_logger import CoralogixLogger

PRIVATE_KEY = "[PUT_YOUR_KEY_HERE]"
APP_NAME = "[YOUR_APPLICATION_NAME]"
SUB_SYSTEM = "[YOUR_SUBSYTEM_NAME]"

# Configure Coralogix logger. You need to do it only once per process.
CoralogixLogger.configure(PRIVATE_KEY, APP_NAME, SUB_SYSTEM)

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

# Get a new instance of Coralogix logger.
coralogix_logger = CoralogixLogger.get_logger(SUB_SYSTEM)

# Add coralogix logger as a handler to the standard Python logger.
logger.addHandler(coralogix_logger)
logger.setLevel(logging.DEBUG)

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

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 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 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 book your implementation session, and we will walk you through, step by step.

Signup to Coralogix
WordPress Lightbox