On this page

Logging for Python SDK

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​

1import os
2import logging
3from pubnub.pnconfiguration import PNConfiguration
4from pubnub.pubnub import PubNub
5
6# Set up logging for PubNub at DEBUG level
7logging.basicConfig(level=logging.DEBUG)
8pubnub_logger = logging.getLogger('pubnub')
9pubnub_logger.setLevel(logging.DEBUG)
10
11# Configuration for PubNub instance
12pn_config = PNConfiguration()
13pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo') # Replace 'demo' with your subscribe key from the PubNub Admin Portal
14pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo') # Replace 'demo' with your publish key from the PubNub Admin Portal
15pn_config.user_id = os.getenv('USER_ID', 'my_debug_user_id')
show all 23 lines
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 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.

Protect sensitive data​

Never enable DEBUG logging in production environments that handle sensitive information.

DEBUG logs may expose:

  • Complete request URLs, including Access Manager signature and auth token query parameters used for signed requests
  • Complete response bodies, including the full token string returned by grant_token() calls
  • API keys, user identifiers, and message content

Use DEBUG logging only in development environments, and set the pubnub logger to WARNING or higher in production.

Protocol version logging​

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

[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.