Logging for Rust SDK
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:
use std::env;
use std::error::Error;
use env_logger::{self, Env};
use log::{debug, error, info, warn};
use pubnub::{channel_groups::ChannelGroups, core::PubNubError, Keyset, PubNubClientBuilder};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Configure and initialize the logger before creating the PubNub client
// You can control log level via the RUST_LOG environment variable
// Examples:
// RUST_LOG=debug cargo run # Show debug and higher logs
// RUST_LOG=info cargo run # Show info and higher logs
show all 97 linesLog levels
The Rust SDK uses standard log levels from the log
crate:
Log Level | Description |
---|---|
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:
# Display all logs including debug information
RUST_LOG=debug cargo run
# Display only warnings and errors
RUST_LOG=warn cargo run
# Display all trace logs for the pubnub crate
RUST_LOG=pubnub=trace cargo run
# Display debug logs for pubnub and warning logs for everything else
RUST_LOG=warn,pubnub=debug cargo run
Complete example
Here's a complete example showing how to set up logging with PubNub:
use std::error::Error;
use env_logger;
use log::{debug, error, info, warn};
use pubnub::{PubNubClientBuilder, Keyset};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Initialize the logger first - this should be done before any logging occurs
env_logger::init();
info!("Initializing PubNub client with logging enabled");
// Then initialize PubNub with demo keys
let client = PubNubClientBuilder::with_reqwest_transport()
show all 57 lines