On this page

Logging for Rust SDK

This page explains how to enable logging in the PubNub Rust Software Development Kit (SDK) using standard Rust logging facilities.

How to enable logging

The PubNub Rust SDK uses the standard Rust logging framework (log crate) internally. To capture these logs, you need to initialize a logger implementation in your application.

Setup with env_logger

The simplest way to enable logging is to use the env_logger crate, which is included as a dependency of the SDK:

1use std::env;
2use std::error::Error;
3
4use env_logger::{self, Env};
5use log::{debug, error, info, warn};
6use pubnub::{channel_groups::ChannelGroups, core::PubNubError, Keyset, PubNubClientBuilder};
7use tokio;
8
9#[tokio::main]
10async fn main() -> Result<(), Box<dyn Error>> {
11 // Configure and initialize the logger before creating the PubNub client
12 // You can control log level via the RUST_LOG environment variable
13 // Examples:
14 // RUST_LOG=debug cargo run # Show debug and higher logs
15 // RUST_LOG=info cargo run # Show info and higher logs
show all 97 lines

Log levels

The Rust SDK uses standard log levels from the log crate:

Log LevelDescription
error
Critical errors that may prevent the SDK from functioning correctly
warn
Warning conditions that don't prevent the SDK from functioning but might indicate issues
info
Informational messages about normal operations
debug
Detailed information useful for debugging
trace
Very detailed information about internal operations

Setting log levels

You can control which log messages are displayed by setting the RUST_LOG environment variable:

1# Display all logs including debug information
2RUST_LOG=debug cargo run
3
4# Display only warnings and errors
5RUST_LOG=warn cargo run
6
7# Display all trace logs for the pubnub crate
8RUST_LOG=pubnub=trace cargo run
9
10# Display debug logs for pubnub and warning logs for everything else
11RUST_LOG=warn,pubnub=debug cargo run

Protect sensitive data

The PubNub Rust SDK logs network activity at the info level, not just debug/trace. Enabling info-level logs is enough to expose sensitive data. Never enable info, debug, or trace logging for the PubNub Rust SDK crate in production environments that handle sensitive information.

These levels may expose:

  • Complete request URLs, including Access Manager signature, auth, and timestamp 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

Network protocol logging

Starting in version 0.8.0, the default TransportReqwest logs the negotiated HTTP protocol at the debug level after each completed request. This applies to both the native async and blocking transports. On wasm32 targets, the protocol is negotiated by the host's environment (e.g. browser) fetch stack rather than the SDK, so this log entry doesn't reliably reflect the negotiated HTTP version.

Each log entry includes the HTTP method, request path, and the protocol version reported by reqwest:

Request completed: method=GET path=/v2/subscribe/demo/demo/0/my_channel/0 protocol=HTTP/2.0

To see protocol logs without enabling debug output for the entire SDK:

1RUST_LOG=pubnub::transport::reqwest=debug cargo run
2
3# Combine with other modules as needed:
4
5RUST_LOG=pubnub::transport::reqwest=debug,pubnub::core::event_engine=debug cargo run

Protocol logging is useful when validating HTTP/2 negotiation against supported origins (for example, https://h2.pubnubapi.com) or when troubleshooting subscribe reconnects where the negotiated protocol may change. For HTTP/2 behavior and reconnect details, refer to HTTP/2 transport.

Complete example

Here's a complete example showing how to set up logging with PubNub:

1use std::error::Error;
2use env_logger;
3use log::{debug, error, info, warn};
4use pubnub::{PubNubClientBuilder, Keyset};
5use tokio;
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn Error>> {
9 // Initialize the logger first - this should be done before any logging occurs
10 env_logger::init();
11
12 info!("Initializing PubNub client with logging enabled");
13
14 // Then initialize PubNub with demo keys
15 let client = PubNubClientBuilder::with_reqwest_transport()
show all 57 lines