---
source_url: https://www.pubnub.com/docs/sdks/asyncio/logging
title: Logging for Python-Asyncio SDK
updated_at: 2026-05-20T11:06:11.330Z
sdk_name: PubNub Python Asyncio SDK
---

> 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-Asyncio SDK

PubNub Python Asyncio SDK

Install:

```bash
pip install pubnub
```

This page explains how to enable logging and handle errors in the PubNub Python-Asyncio Software Development Kit (SDK). Start by enabling basic logs, then learn two error-handling patterns: `future()` and `result()`.

## How to enable logging

```python
import os
import asyncio
import logging
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio
from pubnub.exceptions import PubNubException

async def demonstrate_logging(pubnub: PubNubAsyncio):
    try:
        # This operation will generate logs we can observe
        envelope = await pubnub.publish() \
            .channel("my_channel") \
            .message({'name': 'Alex', 'action': 'logging_demo'}) \
            .future()
        print(f"Publish timetoken: {envelope.result.timetoken}")
    except PubNubException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    # Set up logging
    logging.basicConfig(level=logging.INFO)
    
    # Enable detailed PubNub DEBUG logs
    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 PubNub Admin Portal
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')      # Replace 'demo' with your publish key from PubNub Admin Portal
    pn_config.user_id = "logging_demo_user"

    # Create and configure the event loop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    
    # Initialize PubNub with logging enabled
    pubnub = PubNubAsyncio(pn_config)
    
    try:
        # Run the demo that will generate logs
        loop.run_until_complete(demonstrate_logging(pubnub))
    finally:
        # Clean up resources
        loop.run_until_complete(pubnub.stop())
        loop.close()
```

To see how logging integrates with subscription operations, refer to the [subscribe example](https://www.pubnub.com/docs/sdks/asyncio/api-reference/publish-and-subscribe#basic-subscribe-with-logging). The example sets a stream logger at the DEBUG level and subscribes to a channel using a listener.

## Error handling with future()

For `future()` calls, errors are wrapped in either `AsyncioEnvelope` or `PubNubAsyncioException`. Both expose two fields:

* `e.result`: The request result object if the operation succeeds; otherwise `None`.
* `e.status`: A `PNStatus` object with details about the finished request. Use the `e.is_error()` helper to determine success or failure.

#### Future() usage

```python
import asyncio
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio
from pubnub.exceptions import PubNubException

async def publish_future(pubnub: PubNubAsyncio):
    try:
        envelope = await pubnub.publish().channel("my_channel").message("hello!").future()
        if envelope.status.is_error():
            print(f"Error: {envelope.status.error_data}")
            print(f"Error category #{envelope.status.category}")
            return
        else:
            print(f"Publish successful: {envelope.result}")
    except PubNubException as e:
        print(f"Publish failed: {e}")

if __name__ == "__main__":
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')  # Replace 'demo' with your subscribe key from PubNub Admin Portal
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')      # Replace 'demo' with your publish key from PubNub Admin Portal
    pn_config.user_id = "publish_demo_user"

    # Create and configure the event loop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    
    # Initialize PubNub
    pubnub = PubNubAsyncio(pn_config)
    
    try:
        # Run the publish operation
        loop.run_until_complete(publish_future(pubnub))
    finally:
        # Clean up resources
        loop.run_until_complete(pubnub.stop())
        loop.close()
```

## Error handling with result()

`result()` returns only the request result, without state information. Catch errors with `try`/`except`. For server-side errors, the `e.status` field provides additional information about the request:

```python
import asyncio
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_asyncio import PubNubAsyncio
from pubnub.exceptions import PubNubException

async def publish_result(pubnub: PubNubAsyncio):
    try:
        result = await pubnub.publish().channel("my_channel").message("hello!").result()
        print(f"Publish successful: {result}")
    except PubNubException as e:
        print(f"Error: {e}")
        if e.status is not None:
            print(f"Error category #{e.status.category}")

if __name__ == "__main__":
    # Configuration for PubNub instance
    pn_config = PNConfiguration()
    pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo')  # Replace 'demo' with your subscribe key from PubNub Admin Portal
    pn_config.publish_key = os.getenv('PUBLISH_KEY', 'demo')      # Replace 'demo' with your publish key from PubNub Admin Portal
    pn_config.user_id = "publish_demo_user"

    # Create and configure the event loop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    
    # Initialize PubNub
    pubnub = PubNubAsyncio(pn_config)
    
    try:
        # Run the publish operation
        loop.run_until_complete(publish_result(pubnub))
    finally:
        # Clean up resources
        loop.run_until_complete(pubnub.stop())
        loop.close()
```