---
source_url: https://www.pubnub.com/docs/sdks/python/logging
title: Logging for Python SDK
updated_at: 2026-06-24T11:07:34.586Z
sdk_name: PubNub Python SDK
sdk_version: 10.7.1
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Logging for Python SDK

PubNub Python SDK, use the latest version: 10.7.1

Install:

```bash
pip install pubnub@10.7.1
```

This page explains how to enable logging in the PubNub Python Software Development Kit (SDK).

## How to enable logging

#### Use this code to enable logging

```python
import os
import logging
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub

# Set up logging for PubNub at DEBUG level
logging.basicConfig(level=logging.DEBUG)
pubnub_logger = logging.getLogger('pubnub')
pubnub_logger.setLevel(logging.DEBUG)

# Configuration for PubNub instance
pn_config = PNConfiguration()
pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')  # Replace 'demo' with your subscribe key from the PubNub Admin Portal
pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')  # Replace 'demo' with your publish key from the PubNub Admin Portal
pn_config.user_id = os.getenv('USER_ID', 'my_debug_user_id')

# Initialize PubNub client with debug logging enabled
pubnub = PubNub(pn_config)

# Now any operations with this PubNub instance will produce debug logs
# For example:
pubnub.time().sync()
print("Debug logging is now enabled for PubNub operations")
```

:::note Package-level function call
You should call the `set_stream_logger()` function on the package level and not the PubNub instance.
:::

For a practical example of enabling logging with subscription operations, see the [Basic subscribe with logging](https://www.pubnub.com/docs/sdks/python/api-reference/publish-and-subscribe#basic-subscribe-with-logging) section in the Publish and Subscribe document. The example sets a stream logger at the DEBUG level and subscribes to a channel using a listener.

## Protocol version logging

When DEBUG logging is enabled, each completed request includes the negotiated HTTP protocol version alongside the response body:

```text
[HTTP/2] {"t":{"t":"...","r":12},"m":[]}
[HTTP/1.1] [1,"Sent","..."]
```

This is useful for confirming whether HTTP/2 was successfully negotiated on a given origin. `HttpxRequestHandler` negotiates HTTP/2 automatically; `RequestsRequestHandler` reports the negotiated version but only connects over HTTP/1.1. For details on which origins support HTTP/2, refer to [HTTP/2](https://www.pubnub.com/docs/general/setup/data-transport#http2).