Logging for Kotlin 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.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.v2.PNConfiguration
fun main() {
println("PubNub Logging Example")
println("======================")
// Configure PubNub with logging enabled
val userId = UserId("loggingDemoUser")
val pnConfiguration = PNConfiguration.builder(userId, "demo").apply {
// Replace "demo" with your Subscribe Key from the PubNub Admin Portal
publishKey = "demo" // Replace with your Publish Key from the PubNub Admin Portal
show all 84 linesConfiguration parameter details
The logVerbosity
parameter is an optional configuration setting with a default value of PNLogVerbosity.NONE
.
Available values:
PNLogVerbosity.NONE
- Disables logging of network calls.PNLogVerbosity.BODY
- Enables logging of network calls, including request and response details.
For more information about this and other configuration options, refer to the Configuration document.
Implement logging on Android
To implement logging on Android you need to add the following dependency to the project.
The logs will appear in Logcat.
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.
log4j-slf4j2
file which acts as a bridge betweenslf4j
andlog4j
.log4j-api
file, which provides the underlying logging framework API.log4j-core
file, which provides the underlying logging framework Implementation.
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 linesSee this example:
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubException
import com.pubnub.api.UserId
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.v2.PNConfiguration
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
object LoggingLog4j2 {
private val logger: Logger = LogManager.getLogger(LoggingLog4j2::class.java)
@JvmStatic
fun main(args: Array<String>) {
// Configure PubNub with logging enabled
logger.info("Initializing PubNub with logging enabled")
show all 56 linesImplement logging using slf4j
To implement logging using slf4j
you need to add the following references to the project.
SLF4J API
dependencySLF4J-Simple
dependency which provides the underlying logging framework
Along with these references you need to add the simplelogger.properties
file in the CLASSPATH
.
See this example:
# Set default logging level
org.slf4j.simpleLogger.defaultLogLevel=debug
# Set PubNub okhttp network calls logger to debug
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 linesExample usage of slf4j-simple
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubException
import com.pubnub.api.UserId
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.v2.PNConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object LoggingSlf4jSimpl {
private val logger: Logger = LoggerFactory.getLogger(LoggingSlf4jSimpl::class.java)
@JvmStatic
fun main(args: Array<String>) {
// Configure PubNub with logging enabled
logger.info("Initializing PubNub with logging enabled")
show all 52 linesImplement logging using logback-classic
To implement logging using logback, you need to add the following references to the project.
Logback classic
andlogback-core
jar files which provide the underlying logging frameworkLogback core
dependency which provides the underlying logging framework
See an sample logback.xml
logback config 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 linesSee this example:
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubException
import com.pubnub.api.UserId
import com.pubnub.api.enums.PNLogVerbosity
import com.pubnub.api.v2.PNConfiguration
import org.slf4j.Logger
import org.slf4j.LoggerFactory
object LoggingLogback {
// Get an SLF4j logger instance (named after the class)
private val logger: Logger = LoggerFactory.getLogger(LoggingLogback::class.java)
@JvmStatic
fun main(args: Array<String>) {
logger.info("Starting PubNub logging example...")
show all 60 linesImplement logging using java.util.logging
To implement logging using java.util.logging
you need to add the following references to the project.
SLF4J API
dependencySLF4J-JDK14
dependency which acts as a bridge betweenslf4j
andjava
JVM runtime provides the underlying logging framework.
Implement logging using commons-logging
To implement logging using commons-logging
you need to add the following references to the project.
SLF4J API
dependencySLF4J-JCL
dependency which acts as a bridge between slf4j and common-loggingcommons-logging
dependency which acts as an abstraction layer
The underlying logging framework is chosen dynamically by commons-logging
.
No logging
To implement no logging you have two options:
-
Add the
SLF4J API
dependency which is the SLF4J API and nothing else. -
Add the
SLF4J API
dependency and then add a reference toSLF4J-nop
dependency.