Logging for Unity SDK

How to enable logging

Required UserId

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.

The Unity SDK provides two primary ways to configure logging - by using PNConfigAsset or configuring logging with code.

Use PNConfigAsset in the Unity Editor

The easiest way to configure logging is through the PNConfigAsset in the Unity Editor:

  1. In your Unity project, create a PNConfigAsset by right-clicking in the Project window and selecting Create > PubNub > PubNub Config Asset
  2. Select the created asset and in the Inspector panel:
  • Set LogToUnityConsole to true to enable logging to the Unity console
  • Set LogLevel to an appropriate level like PubnubLogLevel.Debug to control detail level

You can now assign the config to a manager instance inheriting from PNManagerBehaviour and will receive Pubnub logs of the level specified in LogLevel in the Unity console.

Configure logging with code

You can also configure logging directly in your code:

//creating a configuration instance
var configuration = new PNConfiguration(new UserId("some_user")) {
SubscribeKey = "demo",
PublishKey = "demo",
LogLevel = PubnubLogLevel.All
};
//creating a Pubnub instance and doing Unity-specific setup
var pubnub = new Pubnub(configuration, ipnsdkSource: new UnityPNSDKSource());
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(configuration));
//setting the Unity-specific logger
pubnub.SetLogger(new UnityPubNubLogger(pubnub.InstanceId));

Please also note that UnityPubNubLogger is the default implementation of IPubnubLogger provided with the package - you can use a custom logger of your own as long as it implements the IPubnubLogger interface.

View logs

When logging is enabled, messages will appear in the Unity console. The logs include:

  • Timestamps for each log entry
  • A unique instance identifier for the PubNub instance
  • The log level (Trace, Debug, Info, Warn, or Error)
  • The actual log message

For development builds, these logs are also saved to log files that can be accessed as described in the Unity documentation on log files.

Logging options

The Unity SDK provides the following configuration properties to control logging behavior. These options can be set either in the PNConfigAsset through the Unity Inspector or programmatically in your code:

  • LogToUnityConsole: Enable/disable sending logs to the Unity console

  • LogLevel: Controls the level of detail in logs:

    • PubnubLogLevel.None: Disables logging
    • PubnubLogLevel.Error: Shows only errors
    • PubnubLogLevel.Warn: Shows errors and warnings
    • PubnubLogLevel.Info: Shows errors, warnings, and general information
    • PubnubLogLevel.Debug: Shows detailed debug information
    • PubnubLogLevel.Trace: Shows the most detailed level of logs
    • PubnubLogLevel.All: Shows all possible logs

For applications in production, it's recommended to disable detailed logging by setting LogLevel to PubnubLogLevel.None or setting LogToUnityConsole to false to avoid performance impact.

Last updated on