Logging for Java SDK

This page explains how to configure logging in the PubNub Java Software Development Kit (SDK) using SLF4J and popular backends.

How to enable logging

Pubnub implements swappable logging using SLF4J, which allows you to switch different logging frameworks easily. All the logging calls using SLF4J API will be delegated to the underlying logging framework.

Implementations of the SLF4J API satisfy most logging requirements. If you require specific logging functionality, you can create your own logger by implementing the CustomLogger interface. Then, pass the implementation to the SDK with the customLoggers parameter.

For more information, refer to Configuration.

Implement logging using log4j

To implement logging using log4j you need to add the following references to the project. Using log4j you can log to console or a file or both.

To enable network call logging, add the following to the log4j2.xml file:


See this example:


Implement logging using slf4j-simple

To implement logging using slf4j-simple you need to add the following references to the project:

  • slf4j-simple jar file (for example, slf4j-simple-2.0.17.jar or the latest version) which provides the underlying logging framework.
  • Along with these references you need to add the simplelogger.properties file in the CLASSPATH.

Example of simplelogger.properties:


Example usage of slf4j-simple


Implement logging using logback

To implement logging using logback, you need to add the following references to the project:

To enable network call logging, add <logger name="pubnub.okhttp" level="DEBUG"/> to the logback.xml file.

See this example:


Example of the logback.xml file:


Implement logging using java.util.logging

To implement logging using java.util.logging you need to add the following reference to the project:

  • slf4j-jdk14 jar file which acts as a bridge between slf4j and java.

JVM runtime provides the underlying logging framework.

Implement logging using commons-logging

To implement logging using commons-logging you need to add the following reference to the project:

  • slf4j-jcl jar file (for example, slf4j-jcl-1.7.36.jar or the latest version) which acts as a bridge between slf4j and common-logging
  • common-logging.jar file which acts as an abstraction layer.

The underlying logging framework is chosen dynamically by commons-logging.

No logging

To implement no logging, add a reference to slf4j-nop jar file (for example, slf4j-nop-1.7.5.jar or the latest version).

Last updated on