---
source_url: https://www.pubnub.com/docs/sdks/php/logging
title: Logging for PHP SDK
updated_at: 2026-06-26T11:06:23.673Z
sdk_name: PubNub PHP SDK
sdk_version: 9.0.1
---

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

PubNub PHP SDK, use the latest version: 9.0.1

Install:

```bash
composer require pubnub/pubnub@9.0.1
```

This page explains how to enable logging in the PubNub PHP Software Development Kit (SDK) using Monolog.

## How to enable logging

:::note Required UUID
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.
:::

```php
<?php

// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';

use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Formatter\LineFormatter;
use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubException;

/**
 * PubNub Logging Example using Monolog

 * This example demonstrates how to set up logging for PubNub PHP SDK
 * using the Monolog library.
 */

try {
    // Step 1: Create a PubNub configuration
    $pnConfig = new PNConfiguration();
    $pnConfig->setSubscribeKey("demo"); // Replace with your Subscribe Key from the PubNub Admin Portal
    $pnConfig->setPublishKey("demo");   // Replace with your Publish Key from the PubNub Admin Portal
    $pnConfig->setUserId("php-logging-demo-user");
    
    echo "PubNub Configuration created" . PHP_EOL;
    
    // Step 2: Set up Monolog logger
    // Create a logger instance with channel name 'pubnub'
    $logger = new Logger('pubnub');
    
    // Create a formatter for consistent log output
    $dateFormat = "Y-m-d H:i:s";
    $output = "[%datetime%] %level_name%: %message% %context% %extra%\n";
    $formatter = new LineFormatter($output, $dateFormat);
    
    // Add a handler to output logs to stdout (console)
    $stdoutHandler = new StreamHandler('php://stdout', Level::Info);
    $stdoutHandler->setFormatter($formatter);
    $logger->pushHandler($stdoutHandler);
    
    // Add a handler to output logs to PHP error log
    $errorLogHandler = new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Level::Debug);
    $errorLogHandler->setFormatter($formatter);
    $logger->pushHandler($errorLogHandler);
    
    // Optional: Add a file handler to write logs to a file
    $fileHandler = new StreamHandler(__DIR__ . '/pubnub.log', Level::Debug);
    $fileHandler->setFormatter($formatter);
    $logger->pushHandler($fileHandler);
    
    echo "Monolog logger configured with console, error log, and file handlers" . PHP_EOL;
    
    // Step 3: Create PubNub instance and set logger
    $pubnub = new PubNub($pnConfig);
    $pubnub->setLogger($logger);
    
    echo "PubNub instance created and logger attached" . PHP_EOL;
    
    // Step 4: Perform some operations to generate logs
    $logger->info("Performing operations to generate logs");
    
    // Get PubNub time (simple operation to generate logs)
    echo "Fetching PubNub time to generate logs..." . PHP_EOL;
    $timeResult = $pubnub->time()->sync();
    echo "Time result: " . $timeResult->getTimetoken() . PHP_EOL;
    
    // Publish a message (generates more logs)
    echo "Publishing a message to generate more logs..." . PHP_EOL;
    $publishResult = $pubnub->publish()
        ->channel("logging-demo-channel")
        ->message(["text" => "Hello from PHP logging example!"])
        ->sync();
    
    echo "Message published with timetoken: " . $publishResult->getTimetoken() . PHP_EOL;
    
    // Get Here Now data (generates even more logs)
    echo "Fetching Here Now data..." . PHP_EOL;
    $hereNowResult = $pubnub->hereNow()
        ->channels(["logging-demo-channel"])
        ->sync();
    
    echo "Total occupancy: " . $hereNowResult->getTotalOccupancy() . PHP_EOL;
    
    // Log a final message
    $logger->info("Operations completed successfully");
    
    echo PHP_EOL . "Logging example complete!" . PHP_EOL;
    echo "Check the following locations for logs:" . PHP_EOL;
    echo "1. Console output (above)" . PHP_EOL;
    echo "2. PHP error log" . PHP_EOL;
    echo "3. " . __DIR__ . "/pubnub.log" . PHP_EOL;
    
} catch (PubNubException $exception) {
    echo "PubNub Error: " . $exception->getMessage() . PHP_EOL;
} catch (Exception $exception) {
    echo "Error: " . $exception->getMessage() . PHP_EOL;
}
```

## How to disable logging

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:

```php
<?php

// Include Composer autoloader (adjust path if needed)
require_once 'vendor/autoload.php';

use PubNub\PNConfiguration;
use PubNub\PubNub;
use PubNub\Exceptions\PubNubException;

/**
 * Basic PubNub PHP SDK Initialization Example
 */
try {
    // Create a new configuration
    $pnconf = new PNConfiguration();
    
    // Set the required keys and configuration
    $pnconf->setPublishKey("demo");    // Replace with your Publish Key from the PubNub Admin Portal
    $pnconf->setSubscribeKey("demo");  // Replace with your Subscribe Key from the PubNub Admin Portal
    $pnconf->setUserId('php-basic-user');
    
    // Optional: Configure timeouts (in seconds)
    $pnconf->setConnectTimeout(10);
    $pnconf->setNonSubscribeRequestTimeout(10);
    $pnconf->setSubscribeTimeout(310);
    
    // Initialize PubNub with the configuration
    $pubnub = new PubNub($pnconf);
    
    echo "PubNub initialized successfully with the following configuration:" . PHP_EOL;
    echo "- Subscribe Key: " . $pnconf->getSubscribeKey() . PHP_EOL;
    echo "- Publish Key: " . $pnconf->getPublishKey() . PHP_EOL;
    echo "- User ID: " . $pnconf->getUserId() . PHP_EOL;
    
    // Optional: Perform a simple operation to verify the connection
    $timeResult = $pubnub->time()->sync();
    echo "PubNub server time: " . $timeResult->getTimetoken() . PHP_EOL;
    
    echo PHP_EOL . "PubNub SDK is ready to use!" . PHP_EOL;
    
} catch (PubNubException $exception) {
    echo "PubNub Error: " . $exception->getMessage() . PHP_EOL;
} catch (Exception $exception) {
    echo "Error: " . $exception->getMessage() . PHP_EOL;
}
```