On this page

Logging for Java SDK

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

Logging architecture

You can configure logging for different environments and use cases:

  • SLF4J integration: All logs route through SLF4J, allowing you to use any SLF4J-compatible backend (logback, log4j, slf4j-simple, commons-logging, java.util.logging)
  • Custom loggers: Optionally add custom logger implementations via the customLoggers configuration parameter. Use it if your slf4j implementation doesn't meet your needs.

The SDK sends all log entries to both slf4j and any configured custom loggers simultaneously.

Log levels

SLF4J uses standard log levels:

LevelPurpose
TRACE
Internal operations: serialization, encryption, subscription lifecycle, and detailed execution flow
DEBUG
User inputs, API parameters, HTTP requests and responses, and configuration properties
INFO
Significant events like successful initialization and configuration changes
WARN
Deprecation warnings, unusual conditions, and non-breaking validation warnings
ERROR
Errors, exceptions with stack traces, and failed operations

The SDK logs all configuration properties at DEBUG level during initialization. Secret keys are masked.

Custom loggers

You can implement the CustomLogger interface to implement your specific logging requirements, for example, route logs to external monitoring services, databases, or analytics platforms.

CustomLogger interface

The CustomLogger interface supports string messages and structured LogMessage objects:

1public interface CustomLogger {
2 String getName();
3
4 // String-based logging methods
5 void trace(String message);
6 void debug(String message);
7 void info(String message);
8 void warn(String message);
9 void error(String message);
10
11 // Structured logging methods
12 void trace(LogMessage logMessage);
13 void debug(LogMessage logMessage);
14 void info(LogMessage logMessage);
15 void warn(LogMessage logMessage);
show all 17 lines

The SDK calls both overloads for each log entry:

  • String version - a simplified, human-readable message
  • LogMessage version - structured data including instance ID, timestamp, location, and typed content

All methods have default no-op implementations, so you only need to override the ones you plan to use.

Structured logging with LogMessage

LogMessage provides structured information for each log entry:

FieldDescription
message
The log content (one of: Text, Object, Error, NetworkRequest, or NetworkResponse)
details
Optional additional context
type
Automatically inferred from message content (TEXT, OBJECT, ERROR, NETWORK_REQUEST, NETWORK_RESPONSE)
location
Source class or method generating the log
pubNubId
PubNub instance identifier
logLevel
The SLF4J log level (TRACE, DEBUG, INFO, WARN, ERROR)
timestamp
When the log was created with millisecond precision (format: HH:mm:ss.SSS)

Custom logger example

1

Configure the SDK with your custom logger:

1

SLF4J backend configuration

The following sections explain how to configure different SLF4J backends. Choose the backend that best fits your environment and logging requirements.

For more information about configuration parameters, 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.

Configure the log4j2.xml file:

1

See this example:

1

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:

1

Example usage of slf4j-simple

1

Implement logging using logback

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

See this example:

1

Example of the logback.xml file:

1

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