Skip to content

Latest commit

 

History

History
134 lines (91 loc) · 7.94 KB

azure-sdk-logging.md

File metadata and controls

134 lines (91 loc) · 7.94 KB
title description ms.date ms.topic ms.custom
Configure logging in the Azure libraries for Python
The Azure libraries use the standard Python logging module, which is configured on a per-library or per-operation basis.
05/20/2021
conceptual
devx-track-python

Configure logging in the Azure libraries for Python

Azure Libraries for Python that are based on azure.core page provide logging output using the standard Python logging 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 a logging_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 at github.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

:::code language="python" source="~/../python-sdk-docs-examples/logging/set_levels.py" range="1-9":::

  • This example acquires the logger for the azure.mgmt.resource library, then sets the logging level to logging.DEBUG.
  • You can call logger.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 the logging.getLogger call. For example, the azure-eventhubs library provides a logger named azure.eventhubs, the azure-storage-queue library provides a logger named azure.storage.queue, and so on. (The SDK source code frequently uses the statement logging.getLogger(__name__), which acquires a logger using the name of the containing module.)

You can also use more general namespaces. For example,

:::code language="python" source="~/../python-sdk-docs-examples/logging/set_levels.py" range="1-2,11-17":::

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

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

:::code language="python" source="~/../python-sdk-docs-examples/storage/use_blob_auth_logging.py" range="10-13":::

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

Logging level Typical use
logging.ERROR Failures 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.INFO Function 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.DEBUG Detailed 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 set logging_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.NOTSET Disable 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 very little.

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

  1. In the repository folder, navigate into the sdk 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:

:::code language="python" source="~/../python-sdk-docs-examples/storage/use_blob_auth_logging.py" range="3,18-22":::

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

Enable HTTP logging for a client object or operation

By default, logging within the Azure libraries does not include any HTTP information. To include HTTP information in log output (as DEBUG level), you must specifically pass logging_enable=True to a client or credential object constructor or to a specific method.

CAUTION: HTTP logging can reveal includes 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 (DEBUG level)

:::code language="python" source="~/../python-sdk-docs-examples/logging/enable_for_client.py":::

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

Enable HTTP logging for a credential object (DEBUG level)

:::code language="python" source="~/../python-sdk-docs-examples/logging/enable_for_credential.py":::

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

Enable logging for an individual method (DEBUG level)

:::code language="python" source="~/../python-sdk-docs-examples/logging/enable_for_method.py":::

Example logging output

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

:::code language="python" source="~/../python-sdk-docs-examples/storage/use_blob_auth_logging.py" range="1-9,15-32":::

The logging output is as follows:

Request URL: 'https://pythonsdkstorage12345.blob.core.windows.net/blob-container-01/sample-blob.txt'
Request method: 'PUT'
Request headers:
    'Content-Type': 'application/octet-stream'
    'Content-Length': '79'
    'x-ms-version': '2019-07-07'
    'x-ms-blob-type': 'BlockBlob'
    'If-None-Match': '*'
    'x-ms-date': 'Mon, 01 Jun 2020 22:54:14 GMT'
    'x-ms-client-request-id': 'd081f88e-a45a-11ea-b9eb-0c5415dfd03a'
    'User-Agent': 'azsdk-python-storage-blob/12.3.1 Python/3.8.3 (Windows-10-10.0.18362-SP0)'
    'Authorization': '*****'
Request body:
b"Hello there, Azure Storage. I'm a friendly file ready to be stored in a blob.\r\n"
Response status: 201
Response headers:
    'Content-Length': '0'
    'Content-MD5': 'kvMIzjEi6O8EqTVnZJNakQ=='
    'Last-Modified': 'Mon, 01 Jun 2020 22:54:14 GMT'
    'ETag': '"0x8D8067EB52FF7BC"'
    'Server': 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0'
    'x-ms-request-id': '5df479b1-f01e-00d0-5b67-382916000000'
    'x-ms-client-request-id': 'd081f88e-a45a-11ea-b9eb-0c5415dfd03a'
    'x-ms-version': '2019-07-07'
    'x-ms-content-crc64': 'QmecNePSHnY='
    'x-ms-request-server-encrypted': 'true'
    'Date': 'Mon, 01 Jun 2020 22:54:14 GMT'
Response content: