{"id":3545,"library":"logging-azure-rest","title":"Azure REST API Logging Handler","description":"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.","status":"active","version":"1.3.0","language":"en","source_language":"en","source_url":"https://github.com/Exelscior/logging-azure-rest","tags":["logging","azure","log analytics","rest api","monitor","cloud"],"install":[{"cmd":"pip install logging-azure-rest","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for making HTTP requests to the Azure Log Workspace REST API.","package":"requests","optional":false},{"reason":"Used for timezone handling.","package":"pytz","optional":false}],"imports":[{"symbol":"AzureLogServiceHandler","correct":"from logging_azure.handler import AzureLogServiceHandler"}],"quickstart":{"code":"import logging\nimport logging.config\nimport os\nimport time\n\n# Set required environment variables for the handler\nos.environ['AZURE_LOG_CUSTOMER_ID'] = os.environ.get('AZURE_LOG_CUSTOMER_ID', 'YOUR_AZURE_LOG_CUSTOMER_ID')\nos.environ['AZURE_LOG_SHARED_KEY'] = os.environ.get('AZURE_LOG_SHARED_KEY', 'YOUR_AZURE_LOG_SHARED_KEY')\nos.environ['AZURE_LOG_DEFAULT_NAME'] = os.environ.get('AZURE_LOG_DEFAULT_NAME', 'MyPythonAppLogs')\n\nLOGGING_CONFIG = {\n    'version': 1,\n    'disable_existing_loggers': False,\n    'formatters': {\n        'azure': {\n            'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'\n        },\n        'default': {\n            'format': '%(levelname)s:%(name)s:%(message)s'\n        }\n    },\n    'handlers': {\n        'console': {\n            'level': 'DEBUG',\n            'class': 'logging.StreamHandler',\n            'formatter': 'default'\n        },\n        'azure_log_oms': {\n            'level': 'INFO',\n            'class': 'logging_azure.handler.AzureLogServiceHandler',\n            'formatter': 'azure'\n        }\n    },\n    'loggers': {\n        'my_app': {\n            'handlers': ['console', 'azure_log_oms'],\n            'level': 'INFO',\n            'propagate': False\n        },\n        '': {\n            'handlers': ['console'],\n            'level': 'WARNING',\n            'propagate': True\n        }\n    },\n    'root': {\n        'handlers': ['console'],\n        'level': 'WARNING'\n    }\n}\n\nlogging.config.dictConfig(LOGGING_CONFIG)\n\nlogger = logging.getLogger('my_app')\n\nlogger.info('This is an informational message sent to Azure Log Analytics.')\nlogger.warning('This is a warning message. Take action!')\nlogger.error('An error occurred in the application.')\n\nprint(\"Logs sent. Check your Azure Log Analytics workspace (table: MyPythonAppLogs_CL).\")\nprint(\"Note: Logs may take up to 30 minutes to appear initially in Azure.\")\n# Give the background thread some time to send logs before exiting\ntime.sleep(10)\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to a version that supports modern TLS (Python 3.7+ usually suffices, ensure OpenSSL is updated) and verify network configurations do not restrict TLS versions.","message":"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.","severity":"breaking","affected_versions":"All versions interacting with Azure Monitor Logs Ingestion API after 2026-03-01"},{"fix":"Set `AZURE_LOG_CUSTOMER_ID`, `AZURE_LOG_SHARED_KEY`, and `AZURE_LOG_DEFAULT_NAME` environment variables before initializing the logger.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always append `_CL` to your custom log table name when performing queries in Azure Log Analytics (e.g., `MyPythonAppLogs_CL | ...`).","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Configure your `AzureLogServiceHandler` with a suitable `level` (e.g., `logging.INFO` or higher) and ensure specific loggers do not propagate `DEBUG` messages to it in production environments.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be patient when first setting up and sending custom logs; check back after 30 minutes if logs are not immediately visible. For ongoing logging, expect a few minutes of latency.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}