---
source_url: https://www.pubnub.com/docs/sdks/ruby/logging
title: Logging for Ruby SDK
updated_at: 2026-05-22T11:07:48.122Z
sdk_name: PubNub Ruby SDK
sdk_version: 6.0.2
---

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

PubNub Ruby SDK, use the latest version: 6.0.2

Install:

```bash
gem install pubnub@6.0.2
```

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

## How to enable logging

By default the PubNub SDK keeps logs in `pubnub.log` located in the script directory. You can pass a custom logger as `:logger` parameter during initialization of the client.

:::note Required User ID
Always set the `UserId` to uniquely identify the user or device that connects to PubNub. This `UserId` should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the `UserId`, you won't be able to connect to PubNub.
:::

```ruby
require 'pubnub'
require 'logger'

def setup_logger
  # Create a logger that outputs to STDOUT (console)
  logger = Logger.new(STDOUT)
  
  # Set the log level
  # Available levels: DEBUG, INFO, WARN, ERROR, FATAL
  logger.level = Logger::DEBUG
  
  # Customize the log format
  logger.formatter = proc do |severity, datetime, progname, msg|
    date_format = datetime.strftime("%Y-%m-%d %H:%M:%S")
    "[#{date_format}] #{severity}: #{msg}\n"
  end
  
  return logger
end

def perform_operations(pubnub)
  puts "Performing operations to generate logs..."
  
  # Time operation - generates logs
  pubnub.time do |envelope|
    puts "Time operation completed with status: #{envelope.status[:code]}"
  end
  
  # Publish operation - generates more detailed logs
  pubnub.publish(
    channel: 'logging-demo-channel',
    message: { text: 'Hello from Ruby logging example!' }
  ) do |envelope|
    puts "Publish operation completed with status: #{envelope.status[:code]}"
  end
  
  # Subscribe to a channel - generates continuous logs
  pubnub.subscribe(
    channels: ['logging-demo-channel']
  )
  
  puts "Operations initiated. Check the logs for details."
  sleep 3 # Wait to see some subscribe logs
  
  # Unsubscribe
  pubnub.unsubscribe(
    channels: ['logging-demo-channel']
  )
end

def main
  # Set up a logger
  logger = setup_logger
  logger.info("Logger initialized")
  
  # Initialize PubNub with the logger
  pubnub = Pubnub.new(
    subscribe_key: ENV.fetch('SUBSCRIBE_KEY', 'demo'), # Replace 'demo' with your Subscribe Key from the PubNub Admin Portal
    publish_key: ENV.fetch('PUBLISH_KEY', 'demo'),     # Replace 'demo' with your Publish Key from the PubNub Admin Portal
    user_id: 'ruby-logging-demo-user',
    ssl: true,
    logger: logger  # Attach the logger to PubNub
  )
  
  logger.info("PubNub initialized with DEBUG level logging")
  
  begin
    # Perform operations to generate logs
    perform_operations(pubnub)
  rescue => e
    logger.error("Error occurred: #{e.message}")
    logger.debug(e.backtrace.join("\n"))
  end
  
  # Clean up
  pubnub.shutdown
  logger.info("PubNub client shutdown")
end

if __FILE__ == $0
  puts "Starting PubNub logging example"
  main
  puts "Logging example completed"
end
```

:::tip Configuration parameter
The `logger` parameter is one of many available configuration options when initializing the PubNub client. For a complete list of configuration parameters and their descriptions, see the [Configuration](https://www.pubnub.com/docs/sdks/ruby/api-reference/configuration#parameters) document.
:::