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
customLoggersconfiguration parameter. Use it if yourslf4jimplementation 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:
| Level | Purpose |
|---|---|
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 linesThe 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:
| Field | Description |
|---|---|
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
Enable logging
Logging is automatically enabled when you create a PubNub instance. The SDK logs configuration details at the DEBUG level during initialization. SLF4J routes all logs to your configured backend.
1
The SDK automatically logs the complete configuration at initialization, including:
- User ID and subscribe key
- Publish key (if set)
- Secret key status (partially masked as first 8 characters plus length, for example
a1b2c3d4… (len=44), if configured) - Connection timeouts and retry settings
- Presence and heartbeat configuration
- Custom logger count (if configured)
- Instance identifier
Secret keys are only partially masked
The one-time configuration log at initialization shows only the first 8 characters and total length of the secret key and auth token (for example, a1b2c3d4… (len=44)), not a full mask. These same values are not masked at all in network request and response logs. See Protect sensitive data.
Logged information
The Java SDK logs information at various stages of operation. This provides complete visibility into SDK behavior.
Configuration at initialization
When you create a PubNub instance, the SDK logs all configuration properties at the DEBUG level. The SDK also logs any subsequent changes to these properties.
Each configuration log entry includes:
- Instance identifier (unique ID)
- SDK version information
- All configuration property names and values
- Masked sensitive values (secret keys, authentication tokens)
API call parameters
The SDK logs user-provided input data for each API call at the DEBUG level.
Logged parameters include:
- Channel names and channel groups
- Messages and metadata
- Timetoken values
- Filter expressions
- Custom query parameters
- Operation-specific parameters
These logs help identify mismatches. You can compare expected input with actual data passed to endpoints.
Network requests and responses
The SDK logs complete HTTP transaction information at the DEBUG level:
Request logs include
- HTTP method (GET, POST, PUT, PATCH, DELETE)
- Complete URL with origin and path
- Query parameters as key-value pairs
- Request headers
- Request body content (for POST, PUT, or PATCH requests), for JSON and text bodies that fit within
loggedHttpResponseMaxBytes. Other bodies are described by size and content type only. - Request identifier (if configured)
Response logs include
- HTTP status code
- Response headers
- Response body content (JSON and text bodies as text, including vendor types that end in
+json, such as the DataSync responses. Other bodies are Base64-encoded.) - Request URL for correlation
- Negotiated protocol (
protocolfield, for exampleh2orhttp/1.1)
Network logs help troubleshoot connectivity issues. Network logs also show exactly what data flows between your application and PubNub servers.
HTTP/2 negotiation logging
Each response log's JSON payload includes a protocol field showing the protocol OkHttp negotiated for that request:
1{
2 "protocol": "h2"
3}
If HTTP/2 wasn't negotiated, protocol is http/1.1 instead. For configuration details, refer to HTTP/2.
Errors and exceptions
The SDK logs errors and exceptions at the ERROR level with:
- Error type (exception class name)
- Error message
- Stack trace (up to 10 frames)
- Duration before failure (for network errors)
- Request details for failed operations
Log content configuration
LogContentConfig controls how much content the SDK renders into each debug log record. It doesn't affect the log source, level, or format. Your SLF4J backend or CustomLogger implementation does that.
Configure it via the logContentConfig() method on PNConfiguration.Builder:
1