Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the UUID, you won't be able to connect to PubNub.
1<?php 2 3// Include Composer autoloader (adjust path if needed) 4require_once'vendor/autoload.php'; 5 6useMonolog\Level; 7useMonolog\Logger; 8useMonolog\Handler\StreamHandler; 9useMonolog\Handler\ErrorLogHandler; 10useMonolog\Formatter\LineFormatter; 11usePubNub\PNConfiguration; 12usePubNub\PubNub; 13usePubNub\Exceptions\PubNubException; 14 15/** 16 * PubNub Logging Example using Monolog 17 * 18 * This example demonstrates how to set up logging for PubNub PHP SDK 19 * using the Monolog library. 20 */ 21 22try{ 23// Step 1: Create a PubNub configuration 24$pnConfig=newPNConfiguration(); 25$pnConfig->setSubscribeKey("demo");// Replace with your Subscribe Key from the PubNub Admin Portal 26$pnConfig->setPublishKey("demo");// Replace with your Publish Key from the PubNub Admin Portal 27$pnConfig->setUserId("php-logging-demo-user"); 28 29echo"PubNub Configuration created".PHP_EOL; 30 31// Step 2: Set up Monolog logger 32// Create a logger instance with channel name 'pubnub' 33$logger=newLogger('pubnub'); 34 35// Create a formatter for consistent log output 36$dateFormat="Y-m-d H:i:s"; 37$output="[%datetime%] %level_name%: %message% %context% %extra%\n"; 38$formatter=newLineFormatter($output,$dateFormat); 39 40// Add a handler to output logs to stdout (console) 41$stdoutHandler=newStreamHandler('php://stdout',Level::Info); 42$stdoutHandler->setFormatter($formatter); 43$logger->pushHandler($stdoutHandler); 44 45// Add a handler to output logs to PHP error log 46$errorLogHandler=newErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM,Level::Debug); 47$errorLogHandler->setFormatter($formatter); 48$logger->pushHandler($errorLogHandler); 49 50// Optional: Add a file handler to write logs to a file 51$fileHandler=newStreamHandler(__DIR__.'/pubnub.log',Level::Debug); 52$fileHandler->setFormatter($formatter); 53$logger->pushHandler($fileHandler); 54 55echo"Monolog logger configured with console, error log, and file handlers".PHP_EOL; 56 57// Step 3: Create PubNub instance and set logger 58$pubnub=newPubNub($pnConfig); 59$pubnub->setLogger($logger); 60 61echo"PubNub instance created and logger attached".PHP_EOL; 62 63// Step 4: Perform some operations to generate logs 64$logger->info("Performing operations to generate logs"); 65 66// Get PubNub time (simple operation to generate logs) 67echo"Fetching PubNub time to generate logs...".PHP_EOL; 68$timeResult=$pubnub->time()->sync(); 69echo"Time result: ".$timeResult->getTimetoken().PHP_EOL; 70 71// Publish a message (generates more logs) 72echo"Publishing a message to generate more logs...".PHP_EOL; 73$publishResult=$pubnub->publish() 74->channel("logging-demo-channel") 75->message(["text"=>"Hello from PHP logging example!"]) 76->sync(); 77 78echo"Message published with timetoken: ".$publishResult->getTimetoken().PHP_EOL; 79 80// Get Here Now data (generates even more logs) 81echo"Fetching Here Now data...".PHP_EOL; 82$hereNowResult=$pubnub->hereNow() 83->channels(["logging-demo-channel"]) 84->sync(); 85 86echo"Total occupancy: ".$hereNowResult->getTotalOccupancy().PHP_EOL; 87 88// Log a final message 89$logger->info("Operations completed successfully"); 90 91echoPHP_EOL."Logging example complete!".PHP_EOL; 92echo"Check the following locations for logs:".PHP_EOL; 93echo"1. Console output (above)".PHP_EOL; 94echo"2. PHP error log".PHP_EOL; 95echo"3. ".__DIR__."/pubnub.log".PHP_EOL; 96 97}catch(PubNubException$exception){ 98echo"PubNub Error: ".$exception->getMessage().PHP_EOL; 99}catch(Exception$exception){ 100echo"Error: ".$exception->getMessage().PHP_EOL; 101}
You can disable the STDOUT print statements by ommiting configuration of logger. Default PSR/Log/NullLogger will be used.
Using this code sample you can discard all log message output:
1<?php 2 3// Include Composer autoloader (adjust path if needed) 4require_once'vendor/autoload.php'; 5 6usePubNub\PNConfiguration; 7usePubNub\PubNub; 8usePubNub\Exceptions\PubNubException; 9 10/** 11 * Basic PubNub PHP SDK Initialization Example 12 */ 13try{ 14// Create a new configuration 15$pnconf=newPNConfiguration(); 16 17// Set the required keys and configuration 18$pnconf->setPublishKey("demo");// Replace with your Publish Key from the PubNub Admin Portal 19$pnconf->setSubscribeKey("demo");// Replace with your Subscribe Key from the PubNub Admin Portal 20$pnconf->setUserId('php-basic-user'); 21 22// Optional: Configure timeouts (in seconds) 23$pnconf->setConnectTimeout(10); 24$pnconf->setNonSubscribeRequestTimeout(10); 25$pnconf->setSubscribeTimeout(310); 26 27// Initialize PubNub with the configuration 28$pubnub=newPubNub($pnconf); 29 30echo"PubNub initialized successfully with the following configuration:".PHP_EOL; 31echo"- Subscribe Key: ".$pnconf->getSubscribeKey().PHP_EOL; 32echo"- Publish Key: ".$pnconf->getPublishKey().PHP_EOL; 33echo"- User ID: ".$pnconf->getUserId().PHP_EOL; 34 35// Optional: Perform a simple operation to verify the connection 36$timeResult=$pubnub->time()->sync(); 37echo"PubNub server time: ".$timeResult->getTimetoken().PHP_EOL; 38 39echoPHP_EOL."PubNub SDK is ready to use!".PHP_EOL; 40 41}catch(PubNubException$exception){ 42echo"PubNub Error: ".$exception->getMessage().PHP_EOL; 43}catch(Exception$exception){ 44echo"Error: ".$exception->getMessage().PHP_EOL; 45}