File: network_activity_logging.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (68 lines) | stat: -rw-r--r-- 2,630 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: network_activity_logging.py

DESCRIPTION:
    This example shows how to enable logging to console, using the storage
    library as an example. This sample expects that the
    `STORAGE_CONNECTION_STRING` environment variable is set.
    It SHOULD NOT be hardcoded in any code derived from this sample.

USAGE: python network_activity_logging.py

    Set the environment variables with your own values before running the sample:
    1) STORAGE_CONNECTION_STRING - the connection string to your storage account

EXAMPLE OUTPUT:
Request with logging enabled and log level set to DEBUG.
Queue test
... <logged network activity> ...
  Message: b'here is a message'
  Message: Here is a non-base64 encoded message.
"""

import base64
import binascii
import logging

import os
import sys

from azure.storage.queue import QueueServiceClient

# Retrieve connection string from environment variables
# and construct a blob service client.
connection_string = os.environ.get("STORAGE_CONNECTION_STRING", None)
if not connection_string:
    print("STORAGE_CONNECTION_STRING required.")
    sys.exit(1)
service_client = QueueServiceClient.from_connection_string(connection_string)

# Retrieve a compatible logger and add a handler to send the output to console (STDOUT).
# Compatible loggers in this case include `azure` and `azure.storage`.
logger = logging.getLogger("azure.storage.queue")
logger.addHandler(logging.StreamHandler(stream=sys.stdout))

# Logging policy logs network activity at the DEBUG level. Set the level on the logger prior to the call.
logger.setLevel(logging.DEBUG)

# The logger level must be set to DEBUG, AND the following must be true:
# `logging_enable=True` passed as kwarg to the client constructor OR the API call
print("Request with logging enabled and log level set to DEBUG.")
queues = service_client.list_queues(logging_enable=True)
for queue in queues:
    print("Queue: {}".format(queue.name))
    queue_client = service_client.get_queue_client(queue.name)
    messages = queue_client.peek_messages(max_messages=20, logging_enable=True)
    for message in messages:
        try:
            print(" Message: {!r}".format(base64.b64decode(message.content)))
        except (binascii.Error, ValueError) as e:
            print("  Message: {}".format(message.content))