Skip to main content

Azure Datalake Logging

Log directly to Azure Storage services (Table, Blob, and Queue)

Azure Datalake Logging provides Python logging handlers that integrate with Azure Storage services, allowing you to send log data directly to Azure without additional infrastructure.

Features

  • Direct logging to Azure Storage services
  • Compatible with standard Python logging
  • Support for structured data logging

Supported Handlers

  • Table Storage
  • Blob Storage (coming soon)
  • Queue Storage (coming soon)

Installation

pip install azure-datalake-logging

Quick Start

Table Storage Example

from os import getenv

from azure.core.credentials import AzureNamedKeyCredential
import azure_datalake_logging as logging

name = __file__.removesuffix(".py")
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)


account_name = getenv("STORAGE_ACCOUNT_NAME")
account_key = getenv("STORAGE_ACCOUNT_KEY")
credential = AzureNamedKeyCredential(account_name, account_key)

table_name = "DatalakeTableHandlerExample"

handler = logging.DatalakeTableHandler(account_name, table_name, credential)
handler.setLevel(logging.INFO)

formatter = logging.JsonFormatter()
handler.setFormatter(formatter)

logger.addHandler(handler)

# Log structured data
data = {"a": 1, "b": 2}
logger.info(data)

# Console output example (using JSONFormatter):
# {"created":"2023-06-15T10:23:45.123456","levelname":"INFO","lineno":29,"message":{"a":1,"b":2},"name":"example"}

# The following data structure is stored in Azure Table Storage.
# Each log entry contains these fields:
{
    "PartitionKey": "2023-06-15",   # Default: today's date
    "RowKey": "unique-id-12345",    # Auto-generated unique ID, UUID7 per default
    "level_number": 20,             # Numeric log level (20 for INFO)
    "level_name": "info",           # Text log level (lowercase)
    "module": "example_module",     # Source module name
    "line_number": 29,              # Source line number
    "function_name": "main",        # Source function name
    "message": "{\"a\":1,\"b\":2}"  # JSON-formatted message
}

Standard Logging Compatibility

Azure Datalake Logging is fully compatible with the standard Python logging module:

# Standard logging setup
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler(f"{name}.log"),
        logging.StreamHandler(),
    ],
)

logging.info("Test")
# 2025-04-06 09:39:56,644 - root - INFO - Test

Configuration

Each handler requires Azure Storage credentials and specific configuration:

Handler Required Parameters
DatalakeTableHandler account_name, table_name

DatalakeTableHandler Implementation Details

The DatalakeTableHandler provides flexible configuration options for logging to Azure Table Storage:

Constructor Parameters

DatalakeTableHandler(
    account_name,             # Required: Azure Storage account name
    table_name,               # Required: Target table name
    credential=None,          # Optional: Azure credential object
    extra=None,               # Optional: `dict` Object with additional information to log
    part_key=None,            # Optional: Custom partition key (defaults to today's date)
    row_key_fn=None,          # Optional: Custom function for generating row keys
    to_stream=True,           # Optional: Whether to also log to console
)

Authentication Options

The handler supports multiple authentication methods:

  • Default Credentials: If no credential is provided, DefaultAzureCredential is used
  • Named Key: Use AzureNamedKeyCredential for account key authentication
  • SAS Token: Use AzureSasCredential for SAS token authentication
  • Token Credential: Use any Azure TokenCredential implementation

Advanced Configuration Examples

Custom Partition and Row Keys:

import uuid
from datetime import datetime

# Use custom partition key (by month) and row key function
handler = logging.DatalakeTableHandler(
    account_name,
    table_name,
    credential=credential,
    part_key=datetime.now().strftime("%Y-%m"),
    row_key_fn=lambda: str(uuid.uuid4())
)

Configure Additional Logging Options:

# Customize file and stream handlers
handler = logging.DatalakeTableHandler(
    account_name,
    table_name,
    credential=credential,
    to_stream=True,
)

Azure Authentication with Named Key:

from azure.core.credentials import AzureNamedKeyCredential

# Using account name and key authentication
credential = AzureNamedKeyCredential(account_name, account_key)
handler = logging.DatalakeTableHandler(account_name, table_name, credential=credential)

Data Storage Format

Logs are stored in Azure Table Storage with the following structure:

  • PartitionKey: Date by default (can be customized)
  • RowKey: Auto-generated unique ID (can be customized)
  • Stored Fields:
    • level_number: Numeric log level
    • level_name: Text log level (lowercase)
    • module: Source module name
    • line_number: Source line number
    • function_name: Source function name
    • message: Structured log message (JSON)

Development Note

This package was developed with assistance from GitHub Copilot for:

  • Generating docstrings
  • Creating test cases

All AI-generated code has been thoroughly reviewed and validated for correctness, security, and performance.


License

MIT License

Copyright (c) 2025 Markus Feiks

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for azure-datalake-logging 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azure-datalake-logging 1.0.1
File Size Uploaded
azure_datalake_logging-1.0.1.tar.gz 62.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for azure-datalake-logging 1.0.1
File Interpreter ABI Platform
azure_datalake_logging-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 77.4 kB

Release files / azure_datalake_logging-1.0.1.tar.gz

Download URL azure_datalake_logging-1.0.1.tar.gz
Size 62.7 kB
Tags Source
SHA-256 checksum
How to use checksums
59b802f92c92e80a2a3bfdd43c09c2b077a08d64cf51aba4506124c98f1b6fca
BLAKE2b-256 checksum
How to use checksums
fc9e20bbbc25602277f3d6084ab234fdfdd4026a4a929bf29e9e7f8f7abc6814
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.2

Release files / azure_datalake_logging-1.0.1-py3-none-any.whl

Download URL azure_datalake_logging-1.0.1-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05ed66ca8039eceabb4edf97117cb360ae085adcb4c6107d6354f288749eab2f
BLAKE2b-256 checksum
How to use checksums
658c2b742d7be5f9181e63893a90b4541689fb5c0dccc78cfe5bd250f84f9af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.2

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page