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. 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()
Parameter | Description |
---|---|
Private Key | Your Coralogix Send-Your-Data API key |
Application Name | The application Tag you wish to append to every log line sent |
SubSystem Name | The 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 location | coralogix_cluster_url |
---|---|
US | api.coralogix.us |
India | api.app.coralogix.in |
Singapore | api.coralogixsg.com |
EU | api.coralogix.com |
EU2 | api.eu2.coralogix.com |
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) ...
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 * @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.