Azure REST API Logging Handler

1.3.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to configure the `logging-azure-rest` handler using Python's `logging.config.dictConfig`. It sets up a logger that outputs to the console and sends INFO, WARNING, and ERROR level messages to Azure Log Analytics. Ensure `AZURE_LOG_CUSTOMER_ID`, `AZURE_LOG_SHARED_KEY`, and `AZURE_LOG_DEFAULT_NAME` environment variables are set with your Azure Log Workspace details. The `AZURE_LOG_DEFAULT_NAME` will appear as 'MyPythonAppLogs_CL' (or whatever you set) in Azure.

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)

view raw JSON →