Azure REST API Logging Handler
logging-azure-rest is a Python library providing a multi-threaded logging handler and service extension for sending application logs to an Azure Log Workspace via its REST API. It offers an asynchronous solution to upload logs, queuing them in a request pool and sending them in bulk, aiming for transparent logging without disrupting the main application process. The current version is 1.3.0, and its release cadence is project-driven, with the last update in June 2021.
Warnings
- breaking The Azure Monitor Logs Ingestion API will enforce TLS 1.2 or higher connections starting March 1, 2026. Ensure your Python environment supports and uses TLS 1.2+ for connections to Azure. Older Python versions or configurations might fail after this date.
- gotcha Three environment variables are required for the handler to function: `AZURE_LOG_CUSTOMER_ID` (your Log Analytics workspace ID), `AZURE_LOG_SHARED_KEY` (one of your workspace keys), and `AZURE_LOG_DEFAULT_NAME` (the base name for your custom log table). If these are not set, the handler will not send logs.
- gotcha The `AZURE_LOG_DEFAULT_NAME` you specify for your custom logs will automatically be suffixed with `_CL` within the Azure Log Workspace (e.g., 'MyPythonAppLogs' becomes 'MyPythonAppLogs_CL'). Remember this suffix when querying your logs in Azure Log Analytics.
- gotcha Sending verbose logs (e.g., `logging.DEBUG`) to Azure Log Workspace can incur significant costs due to data ingestion fees. It is strongly recommended to set an appropriate log level (e.g., `INFO`, `WARNING`, `ERROR`) for your Azure handler to avoid unnecessary costs, especially in production.
- gotcha Custom logs sent to Azure Log Analytics via the REST API can experience an initial delay of up to 30 minutes before they first appear in the workspace. Subsequent logs typically appear within minutes.
Install
-
pip install logging-azure-rest
Imports
- AzureLogServiceHandler
from logging_azure.handler import AzureLogServiceHandler
Quickstart
import logging
import logging.config
import os
import time
# Set required environment variables for the handler
os.environ['AZURE_LOG_CUSTOMER_ID'] = os.environ.get('AZURE_LOG_CUSTOMER_ID', 'YOUR_AZURE_LOG_CUSTOMER_ID')
os.environ['AZURE_LOG_SHARED_KEY'] = os.environ.get('AZURE_LOG_SHARED_KEY', 'YOUR_AZURE_LOG_SHARED_KEY')
os.environ['AZURE_LOG_DEFAULT_NAME'] = os.environ.get('AZURE_LOG_DEFAULT_NAME', 'MyPythonAppLogs')
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'azure': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
},
'default': {
'format': '%(levelname)s:%(name)s:%(message)s'
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default'
},
'azure_log_oms': {
'level': 'INFO',
'class': 'logging_azure.handler.AzureLogServiceHandler',
'formatter': 'azure'
}
},
'loggers': {
'my_app': {
'handlers': ['console', 'azure_log_oms'],
'level': 'INFO',
'propagate': False
},
'': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': True
}
},
'root': {
'handlers': ['console'],
'level': 'WARNING'
}
}
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger('my_app')
logger.info('This is an informational message sent to Azure Log Analytics.')
logger.warning('This is a warning message. Take action!')
logger.error('An error occurred in the application.')
print("Logs sent. Check your Azure Log Analytics workspace (table: MyPythonAppLogs_CL).")
print("Note: Logs may take up to 30 minutes to appear initially in Azure.")
# Give the background thread some time to send logs before exiting
time.sleep(10)