Logging for PHP SDK
This page explains how to enable logging in the PubNub PHP Software Development Kit (SDK) using Monolog.
How to enable logging
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.
1<?php
2
3// Include Composer autoloader (adjust path if needed)
4require_once 'vendor/autoload.php';
5
6use Monolog\Level;
7use Monolog\Logger;
8use Monolog\Handler\StreamHandler;
9use Monolog\Handler\ErrorLogHandler;
10use Monolog\Formatter\LineFormatter;
11use PubNub\PNConfiguration;
12use PubNub\PubNub;
13use PubNub\Exceptions\PubNubException;
14
15/**
show all 101 linesHTTP/2 negotiation logging
After every request, the PHP SDK logs the protocol version that was actually negotiated for that request at debug level:
Publish response from <your-origin> negotiated HTTP/2
If HTTP/2 wasn't negotiated, you'll see negotiated HTTP/1.1 instead. This log line reflects the protocol reported by the response itself, not just what the SDK requested. For configuration details, refer to HTTP/2.
Protect sensitive data
The default PubNub PHP SDK logger only logs endpoint names and connection details, not full request URLs or response bodies. However, if you attach a custom PSR-3 logger or Guzzle middleware, verify it doesn't log full request URLs (which can include Access Manager signature and auth token query parameters for signed requests) or response bodies (which can include the full token string returned by grantToken calls). Review any custom logging integration before enabling it in production.
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:
1<?php
2
3// Include Composer autoloader (adjust path if needed)
4require_once 'vendor/autoload.php';
5
6use PubNub\PNConfiguration;
7use PubNub\PubNub;
8use PubNub\Exceptions\PubNubException;
9
10/**
11 * Basic PubNub PHP SDK Initialization Example
12 */
13try {
14 // Create a new configuration
15 $pnconf = new PNConfiguration();
show all 45 lines