Logging for Java SDK

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.

Set the config property called logVerbosity to PNLogVerbosity.BODY to enable network calls logging. For more info on configuration, check the API reference page.

import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.enums.PNLogVerbosity;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.v2.PNConfiguration;

public class LoggingExample {
public static void main(String[] args) throws PubNubException {
// Configure PubNub with logging enabled
PNConfiguration.Builder configBuilder = PNConfiguration.builder(
new UserId("loggingDemoUser"),
"demo" // Replace with your Subscribe Key from the PubNub Admin Portal
);

// Add publish key (only required if publishing)
show all 49 lines
Additional configuration options

For more detailed information about logging configuration options, including the logVerbosity parameter, refer to the Configuration document.

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 calls logging, set the PubNub config property called logVerbosity to PNLogVerbosity.BODY and add the following entry to the log4j2.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
<!-- Set PubNub logging level -->
<Logger name="com.pubnub" level="trace" additivity="false">
<AppenderRef ref="Console"/>
<!-- <AppenderRef ref="YourCustomAppender"/>-->
show all 23 lines

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:

# Set default logging level
org.slf4j.simpleLogger.defaultLogLevel=debug

# Configure PubNub network calls logging
org.slf4j.simpleLogger.log.pubnub.okhttp=debug

# Show date-time in logs
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss.SSS

# Show thread name
org.slf4j.simpleLogger.showThreadName=true

# Show log name
org.slf4j.simpleLogger.showLogName=true
show all 21 lines

Example usage of slf4j-simple

import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.enums.PNLogVerbosity;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.v2.PNConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingSlf4jSimpl {
// Get an SLF4J logger instance (named after the class)
private static final Logger logger = LoggerFactory.getLogger(LoggingSlf4jSimpl.class);

public static void main(String[] args) throws PubNubException {
// Configure PubNub with logging enabled
logger.info("Initializing PubNub with logging enabled");
show all 53 lines

Implement logging using logback

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

To enable network calls logging, set the PubNub config property called logVerbosity to PNLogVerbosity.BODY and add the following entry to the logback.xml configuration file: <logger name="pubnub.okhttp" level="DEBUG"/>.

See this example:

import com.pubnub.api.PubNubException;
import com.pubnub.api.UserId;
import com.pubnub.api.enums.PNLogVerbosity;
import com.pubnub.api.java.PubNub;
import com.pubnub.api.java.v2.PNConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoggingLogback {
// Get an SLF4j logger instance (named after the class)
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingLogback.class);

public static void main(String[] args) throws PubNubException {
LOGGER.info("Starting PubNub logging example...");

show all 59 lines

Example of the logback.xml logback configuration file:

<configuration>
<!-- Console appender -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- Format: timestamp [thread] level logger - message -->
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- Set the default logging level for all loggers -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

<!-- Configure PubNub network calls logging -->
show all 18 lines

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