Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Configure logging in the Azure libraries for Python

  • 2024-11-01
Feedback

In this article

Azure Libraries for Python that arebased on azure.core provide logging output using the standard Pythonlogging library.

The general process to work with logging is as follows:

  1. Acquire the logging object for the desired library and set the logging level.
  2. Register a handler for the logging stream.
  3. To include HTTP information, pass alogging_enable=True parameter to a client object constructor, a credential object constructor, or to a specific method.

Details are provided in the remaining sections of this article.

As a general rule, the best resource for understanding logging usage within the libraries is to browse the SDK source code atgithub.com/Azure/azure-sdk-for-python. We encourage you to clone this repository locally so you can easily search for details when needed, as the following sections suggest.

Set logging levels

import logging# ...# Acquire the logger for a library (azure.mgmt.resource in this example)logger = logging.getLogger('azure.mgmt.resource')# Set the desired logging levellogger.setLevel(logging.DEBUG)
  • This example acquires the logger for theazure.mgmt.resource library, then sets the logging level tologging.DEBUG.
  • You can calllogger.setLevel at any time to change the logging level for different segments of code.

To set a level for a different library, use that library's name in thelogging.getLogger call. For example, the azure-eventhubs library provides a logger namedazure.eventhubs, the azure-storage-queue library provides a logger namedazure.storage.queue, and so on. (The SDK source code frequently uses the statementlogging.getLogger(__name__), which acquires a logger using the name of the containing module.)

You can also use more general namespaces. For example,

import logging# Set the logging level for all azure-storage-* librarieslogger = logging.getLogger('azure.storage')logger.setLevel(logging.INFO)# Set the logging level for all azure-* librarieslogger = logging.getLogger('azure')logger.setLevel(logging.ERROR)

Theazure logger is used by some libraries instead of a specific logger. For example, the azure-storage-blob library uses theazure logger.

You can use thelogger.isEnabledFor method to check whether any given logging level is enabled:

print(    f"Logger enabled for ERROR={logger.isEnabledFor(logging.ERROR)}, "    f"WARNING={logger.isEnabledFor(logging.WARNING)}, "    f"INFO={logger.isEnabledFor(logging.INFO)}, "    f"DEBUG={logger.isEnabledFor(logging.DEBUG)}")

Logging levels are the same as thestandard logging library levels. The following table describes the general use of these logging levels in the Azure libraries for Python:

Logging levelTypical use
logging.ERRORFailures where the application is unlikely to recover (such as out of memory).
logging.WARNING (default)A function fails to perform its intended task (but not when the function can recover, such as retrying a REST API call). Functions typically log a warning when raising exceptions. The warning level automatically enables the error level.
logging.INFOFunction operates normally or a service call is canceled. Info events typically include requests, responses, and headers. The info level automatically enables the error and warning levels.
logging.DEBUGDetailed information that is commonly used for troubleshooting and includes a stack trace for exceptions. The debug level automatically enables the info, warning, and error levels. CAUTION: If you also setlogging_enable=True, the debug level includes sensitive information such as account keys in headers and other credentials. Be sure to protect these logs to avoid compromising security.
logging.NOTSETDisable all logging.

Library-specific logging level behavior

The exact logging behavior at each level depends on the library in question. Some libraries, such as azure.eventhub, perform extensive logging whereas other libraries do little.

The best way to examine the exact logging for a library is to search for the logging levels in theAzure SDK for Python source code:

  1. In the repository folder, navigate into thesdk folder, then navigate into the folder for the specific service of interest.

  2. In that folder, search for any of the following strings:

    • _LOGGER.error
    • _LOGGER.warning
    • _LOGGER.info
    • _LOGGER.debug

Register a log stream handler

To capture logging output, you must register at least one log stream handler in your code:

import logging# Direct logging output to stdout. Without adding a handler,# no logging output is visible.handler = logging.StreamHandler(stream=sys.stdout)logger.addHandler(handler)

This example registers a handler that directs log output to stdout. You can use other types of handlers as described onlogging.handlers in the Python documentation or use the standardlogging.basicConfig method.

Enable HTTP logging for a client object or operation

By default, logging within the Azure libraries doesn't include any HTTP information. To include HTTP information in log output, you must explicitly passlogging_enable=True to a client or credential object constructor or to a specific method.

Caution

HTTP logging can include sensitive information such as account keys in headers and other credentials. Be sure to protect these logs to avoid compromising security.

Enable HTTP logging for a client object

from azure.storage.blob import BlobClientfrom azure.identity import DefaultAzureCredential# Enable HTTP logging on the client object when using DEBUG level# endpoint is the Blob storage URL.client = BlobClient(endpoint, DefaultAzureCredential(), logging_enable=True)

Enabling HTTP logging for a client object enables logging for all operations invoked through that object.

Enable HTTP logging for a credential object

from azure.storage.blob import BlobClientfrom azure.identity import DefaultAzureCredential# Enable HTTP logging on the credential object when using DEBUG levelcredential = DefaultAzureCredential(logging_enable=True)# endpoint is the Blob storage URL.client = BlobClient(endpoint, credential)

Enabling HTTP logging for a credential object enables logging for all operations invoked through that object, but not for operations in a client object that don't involve authentication.

Enable logging for an individual method

from azure.storage.blob import BlobClientfrom azure.identity import DefaultAzureCredential# endpoint is the Blob storage URL.client = BlobClient(endpoint, DefaultAzureCredential())# Enable HTTP logging for only this operation when using DEBUG levelclient.create_container("container01", logging_enable=True)

Example logging output

The following code is that shown inExample: Use a storage account with the addition of enabling DEBUG and HTTP logging:

import loggingimport osimport sysimport uuidfrom azure.core import exceptionsfrom azure.identity import DefaultAzureCredentialfrom azure.storage.blob import BlobClientlogger = logging.getLogger("azure")logger.setLevel(logging.DEBUG)# Set the logging level for the azure.storage.blob librarylogger = logging.getLogger("azure.storage.blob")logger.setLevel(logging.DEBUG)# Direct logging output to stdout. Without adding a handler,# no logging output is visible.handler = logging.StreamHandler(stream=sys.stdout)logger.addHandler(handler)print(    f"Logger enabled for ERROR={logger.isEnabledFor(logging.ERROR)}, "    f"WARNING={logger.isEnabledFor(logging.WARNING)}, "    f"INFO={logger.isEnabledFor(logging.INFO)}, "    f"DEBUG={logger.isEnabledFor(logging.DEBUG)}")try:    credential = DefaultAzureCredential()    storage_url = os.environ["AZURE_STORAGE_BLOB_URL"]    unique_str = str(uuid.uuid4())[0:5]    # Enable logging on the client object    blob_client = BlobClient(        storage_url,        container_name="blob-container-01",        blob_name=f"sample-blob-{unique_str}.txt",        credential=credential,    )    with open("./sample-source.txt", "rb") as data:        blob_client.upload_blob(data, logging_body=True, logging_enable=True)except (    exceptions.ClientAuthenticationError,    exceptions.HttpResponseError) as e:    print(e.message)

The output is as follows:

Logger enabled for ERROR=True, WARNING=True, INFO=True, DEBUG=TrueRequest URL: 'https://pythonazurestorage12345.blob.core.windows.net/blob-container-01/sample-blob-5588e.txt'Request method: 'PUT'Request headers:    'Content-Length': '77'    'x-ms-blob-type': 'BlockBlob'    'If-None-Match': '*'    'x-ms-version': '2023-11-03'    'Content-Type': 'application/octet-stream'    'Accept': 'application/xml'    'User-Agent': 'azsdk-python-storage-blob/12.19.0 Python/3.10.11 (Windows-10-10.0.22631-SP0)'    'x-ms-date': 'Fri, 19 Jan 2024 19:25:53 GMT'    'x-ms-client-request-id': '8f7b1b0b-b700-11ee-b391-782b46f5c56b'    'Authorization': '*****'Request body:b"Hello there, Azure Storage. I'm a friendly file ready to be stored in a blob."Response status: 201Response headers:    'Content-Length': '0'    'Content-MD5': 'SUytm0872jZh+KYqtgjbTA=='    'Last-Modified': 'Fri, 19 Jan 2024 19:25:54 GMT'    'ETag': '"0x8DC1924749AE3C3"'    'Server': 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0'    'x-ms-request-id': '7ac499fa-601e-006d-3f0d-4bdf28000000'    'x-ms-client-request-id': '8f7b1b0b-b700-11ee-b391-782b46f5c56b'    'x-ms-version': '2023-11-03'    'x-ms-content-crc64': 'rtHLUlztgxc='    'x-ms-request-server-encrypted': 'true'    'Date': 'Fri, 19 Jan 2024 19:25:53 GMT'Response content:b''

Note

If you get an authorization error, make sure the identity you're running under is assigned the "Storage Blob Data Contributor" role on your blob container. To learn more, seeUse blob storage from app code (Passwordless tab).


Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo