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

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
Last updated on