Coralogix Python SDK
The Coralogix Python SDK enables sending logs directly from Python applications to the Coralogix platform. It leverages Python's standard logging library and operates asynchronously using a dedicated thread for log buffering and flushing. The current version is 2.1.1, and the library is actively maintained with regular updates.
Warnings
- breaking As of SDK version 2.1.0, it is required to specify your Coralogix region during initialization. Failing to do so will result in logs not being sent to the correct regional endpoint.
- deprecated Importing `CoralogixLogger` directly from `coralogix.coralogix_logger` is deprecated and will be removed in future versions.
- gotcha When using uWSGI, threading support must be explicitly enabled, as the Coralogix logger relies on a background thread for sending logs.
- gotcha Logs might not be sent immediately upon calling logging methods due to the asynchronous nature of the SDK. For scripts or applications that exit quickly, ensure logs are flushed manually.
Install
-
pip install coralogix-logger
Imports
- CoralogixLogger
from coralogix.handlers import CoralogixLogger
Quickstart
import logging
import os
import json
from coralogix.handlers import CoralogixLogger
PRIVATE_KEY = os.environ.get('CORALOGIX_PRIVATE_KEY', 'YOUR_PRIVATE_KEY')
APP_NAME = os.environ.get('CORALOGIX_APP_NAME', 'my-python-app')
SUB_SYSTEM = os.environ.get('CORALOGIX_SUB_SYSTEM', 'my-subsystem')
REGION = os.environ.get('CORALOGIX_REGION', 'EU1') # e.g., 'EU1', 'US1', 'AP1'
# Get an instance of Python standard logger.
logger = logging.getLogger("Python Logger")
logger.setLevel(logging.DEBUG)
# Get a new instance of Coralogix logger.
# Region is required as of SDK version 2.1.0
coralogix_handler = CoralogixLogger(PRIVATE_KEY, APP_NAME, SUB_SYSTEM, region=REGION)
# Add coralogix logger as a handler to the standard Python logger.
logger.addHandler(coralogix_handler)
# Send messages
logger.info("Hello World from Coralogix Python SDK!")
my_dict = {"host": "localhost", "message": "This is a structured log."}
logger.info(json.dumps(my_dict))
# Manually flush logger to ensure logs are sent before application exit
CoralogixLogger.flush_messages()
print("Logs sent to Coralogix.")