Troubleshooting PubNub Swift SDK
How to enable logging
Use the following code to enable logging:
PubNub.log.levels = [.all]
PubNub.log.writers = [ConsoleLogWriter(), FileLogWriter()]
PubNub log runner
The PubNub log runner is a singleton instance, and should be configured once at the beginning of your application's lifecycle. Changing log properties will overwrite their previous entries.
Using Log Writers
You can configure log levels by assigning the PubNub.log.writers
array property to your desired log levels.
Enabling console and file output
Use the following code to enable console and file output:
PubNub.log.writers = [ConsoleLogWriter(), FileLogWriter()]
By default, the FileLogWriter
attempts to log to the user's cache directory. For more information, refer to the Apple documentation on the domain constants and search paths for the significant directories.
Logging to a Custom Location
Use the following code to enable log file output using a URL:
if let url = URL(string: "file:///path/to/pubnub/logs/") {
PubNub.log.writers = [FileLogWriter(logDirectory: url)]
}
Use the following code to enable file output using FileManager
PubNub.log.writers = [FileLogWriter(inside: .allDomainsMask, at: .applicationDirectory, with: 'directoryName')]
Creating custom log writers
You can create a custom log writer if you want to output logs to a location other than the console or local file system.
To create a custom log writer, implement the LogWriter
protocol, then add an instance of your custom writer to the PubNub.log.writers
property.
Log Level Reference
Log levels are as follows:
Log Level | Bitmask | Description |
---|---|---|
none | 0 | Logging is disabled. |
debug | 1 << 0 | May be useful during development or while troubleshooting a specific problem. Warning! Debug logging is intended for use in a development environment. Don't use it shipping software. |
info | 1 << 1 | Information that may be helpful, but isn't essential, for troubleshooting errors. |
event | 1 << 2 | Logs as part of an event stream |
warn | 1 << 3 | Designates potentially harmful situations. |
error | 1 << 4 | Error-level messages are intended for reporting process-level errors. If an activity object exists, logging at this level captures information for the entire process chain. |
log | 1 << 31 | This is a special category used to log errors that occur internal to the logger. |
all | UInt32.max | All log levels will be captured |
Setting log levels
Configure log levels by assigning the PubNub.log.levels
array property to your desired log levels, as in the following examples.
Enable all logging
PubNub.log.levels = [.all]
Enable only error logging
PubNub.log.levels = [.error]
Enable non-debug level logging
PubNub.log.levels = [.log, .error, .warn, .event, .info]
Disable logging with either of the following
PubNub.log.levels = [.none]
// Or this:
PubNub.log.levels = []