Logging APIs in Kotlin SDK Alpha

Learn about the different logging APIs available in the Kotlin SDK.

This guide walks you through the logging APIs available in the Kotlin SDK.

Overview

The Kotlin SDK provides a flexible and configurable logging mechanism that helps you:

  • Monitor and debug the SDK’s behavior.
  • Get fine-grained control over the amount of information to be recorded by leveraging multiple log levels.
  • Set up and use a custom logging mechanism and log your own messages by leveraging the SDK’s logging APIs.

Set the log level

You can define the log level while initializing the SDK by passing the value for the logLevel parameter. It controls the verbosity of the logs printed by the SDK and helps filter out logs based on their importance.

The Kotlin SDK supports the following log levels:

Log levelDescription
VERBOSELogs all the messages, including detailed internal operations. It is useful for deep debugging.
DEBUGLogs detailed information relevant for debugging and omits the excessive internal logs.
INFOLogs general operational information about the SDK’s execution and helps track high-level flow.
WARNLogs potentially problematic situations that may not cause immediate failures. It is useful for detecting any unusual SDK behavior.
ERRORLogs only serious issues that impact the SDK’s functionality and require attention.
NONEDisables all logging. Use it when logging is unnecessary or needs to be disabled in production.

The following snippet shows you how to set a VERBOSE log level:

analytics = Analytics(
    configuration = Configuration(
        writeKey = BuildConfig.WRITE_KEY,
        application = application,
        dataPlaneUrl = BuildConfig.DATA_PLANE_URL,
        logLevel = Logger.LogLevel.VERBOSE,
    )
)

Set custom logs

The Kotlin SDK provides a LoggerAnalytics instance that you can use to set custom logs for the SDK. It is helpful when the SDK logs need to be printed in a custom plugin.

The SDK provides the following methods for printing different levels of log messages:

// Verbose log message
LoggerAnalytics.verbose("This is a verbose log message providing detailed internal information.")

// Debug log message
LoggerAnalytics.debug("Debugging info: API request started.")

// Info log message
LoggerAnalytics.info("SDK initialized successfully.")

// Warn log message
LoggerAnalytics.warn("Low memory warning detected.")

// Error log message
LoggerAnalytics.error("Failed to fetch user data: Network error.")  

Use a custom logger

You can create and use a custom logger that is used by the SDK to log messages - this is helpful in cases where you don’t want to rely on the SDK’s default logger.

For the Kotlin SDK, you can inherit the Logger interface to create a custom Logger class and then pass its instance to the setLogger method.

For example, if you want to use the Timber library for logging, then you can create a Logger by following these steps:

  1. Create a custom logger, as shown:
class MyCustomLogger : Logger {
    private val tag = "MyCustomTag"

    override fun verbose(log: String) {
        Timber.tag(tag).v(log)
    }

    override fun debug(log: String) {
        Timber.tag(tag).d(log)
    }

    override fun info(log: String) {
        Timber.tag(tag).i(log)
    }

    override fun warn(log: String) {
        Timber.tag(tag).w(log)
    }

    override fun error(log: String, throwable: Throwable?) {
        Timber.tag(tag).e(throwable, log)
    }
}
  1. Add MyCustomLogger to the analytics instance as follows:
analytics.setLogger(MyCustomLogger())

Questions? Contact us by email or on Slack