Azure Log Analytics Client Library (Deprecated)

0.1.1 · deprecated · verified Sat Apr 11

This is an older Microsoft Azure client library for interacting with Azure Log Analytics. It is no longer maintained and has been officially deprecated. For querying data, users are advised to migrate to the `azure-monitor-query` library. For ingesting data into Log Analytics, the `azure-monitor-ingestion` library should be used. The last stable release for this package was 0.1.1 in 2021.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to query Azure Log Analytics using the recommended `azure-monitor-query` client library. It authenticates using `DefaultAzureCredential` (which leverages environment variables or Azure CLI login) and executes a Kusto Query Language (KQL) query against a specified Log Analytics workspace. Replace `<your_log_analytics_workspace_id>` with your actual workspace ID.

import os
from azure.identity import DefaultAzureCredential
from azure.monitor.query import LogsQueryClient
from datetime import timedelta

# Set your Log Analytics Workspace ID
# Can be found in the Azure portal under your Log Analytics workspace's 'Overview' page
LOGS_WORKSPACE_ID = os.environ.get("LOGS_WORKSPACE_ID", "<your_log_analytics_workspace_id>")

# Authenticate using DefaultAzureCredential
# Ensure environment variables AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET are set,
# or you are logged in via 'az login' (Azure CLI) for this to work automatically.
credential = DefaultAzureCredential()

# Create a LogsQueryClient
client = LogsQueryClient(credential)

# Define a Kusto Query Language (KQL) query
query = """
AzureActivity
| summarize count() by ResourceGroup
| limit 5
"""

try:
    # Execute the query for data from the last 1 day
    response = client.query_workspace(
        LOGS_WORKSPACE_ID,
        query,
        timespan=timedelta(days=1)
    )

    # Process and print the results
    if response.tables:
        for table in response.tables:
            print(f"\nTable: {table.name}")
            print(f"Columns: {[col.name for col in table.columns]}")
            for row in table.rows:
                print(row)
    else:
        print("No results found for the query.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →