Learn about the different logging APIs available in the Android (Kotlin) and iOS (Swift) SDKs.
7 minute read
This guide walks you through the logging APIs available in the RudderStack Android (Kotlin) and iOS (Swift) SDKs.
Overview
The RudderStack Android (Kotlin) and iOS (Swift) SDKs provide 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 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 Android (Kotlin) and iOS (Swift) SDKs support the following log levels:
Log level
Description
VERBOSE
Logs all the messages, including detailed internal operations. It is useful for deep debugging.
DEBUG
Logs detailed information relevant for debugging and omits the additional internal logs.
INFO
Logs general operational information about the SDK’s execution and helps track high-level flow.
WARN
Logs potentially problematic situations that may not cause immediate failures. It is useful for detecting any unusual SDK behavior.
ERROR
Logs only serious issues that impact the SDK’s functionality and require attention.
NONE
Disables all logging. Use it when logging is unnecessary or needs to be disabled in production.
The following sections show you how to set a VERBOSE log level in Android (Kotlin) and iOS (Swift) SDKs.
Android (Kotlin)
Instance-based logging in Android (Kotlin) SDK
Starting from v1.5.0, the Android (Kotlin) SDK only supports instance-based logging. You can configure the logger and logLevel directly on the Configuration object while initializing the SDK — this ties the logger to a specific Analytics instance, allowing different instances to use different loggers and log levels.
The older LoggerAnalytics singleton approach is deprecated in Android (Kotlin) SDK v1.5.0+, but will continue to work for backward compatibility.
Instance-based logging (Recommended)
Pass the logLevel parameter when creating the Configuration object:
The Android (Kotlin) and iOS (Swift) SDKs provide a way to print custom logs for the SDK. It is helpful when the SDK logs need to be printed in a custom plugin.
The SDKs provide the following methods for printing different levels of log messages:
Android (Kotlin)
The Android (Kotlin) SDK supports the following ways for printing custom logs:
Use the logger extension property available in the Plugin interface — this gives you the logger tied to the Analytics instance that the plugin belongs to.
The logger is only accessible within a custom plugin via the Plugin.logger extension property. Direct access to analytics.logger outside of a plugin is not part of the public API.
You should access this property only after the plugin’s setup method has been invoked, as it depends on the plugin’s analytics property being initialized.
importcom.rudderstack.sdk.kotlin.core.internals.plugins.Pluginimportcom.rudderstack.sdk.kotlin.core.internals.plugins.loggerclassMyCustomPlugin:Plugin{overridevalpluginType:PluginType=PluginType.PreProcessoverridelateinitvaranalytics:Analyticsoverridesuspendfunintercept(event:Event):Event{logger.verbose("MyCustomPlugin: Processing event (messageId=${event.messageId})")logger.debug("MyCustomPlugin: Debug info for event processing")logger.info("MyCustomPlugin: Important operational info")logger.warn("MyCustomPlugin: Something unexpected happened")logger.error("MyCustomPlugin: Something went wrong")returnevent}}
importcom.rudderstack.sdk.kotlin.core.internals.plugins.PluginKt;importcom.rudderstack.sdk.kotlin.core.internals.plugins.Plugin;publicclassMyCustomPluginimplementsPlugin{// After the plugin is added to Analytics, use PluginKt.getLogger(this)
PluginKt.getLogger(this).verbose("MyCustomPlugin: Processing event");PluginKt.getLogger(this).debug("MyCustomPlugin: Debug info for event processing");PluginKt.getLogger(this).info("MyCustomPlugin: Important operational info");PluginKt.getLogger(this).warn("MyCustomPlugin: Something unexpected happened");PluginKt.getLogger(this).error("MyCustomPlugin: Something went wrong",null);}
via LoggerAnalytics (Deprecated)
This approach is deprecated but continues to work for backward compatibility. Use the logger extension property approach instead.
// 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.")
// Verbose log message
LoggerAnalytics.INSTANCE.verbose("This is a verbose log message providing detailed internal information.")// Debug log message
LoggerAnalytics.INSTANCE.verbose("Debugging info: API request started.")// Info log message
LoggerAnalytics.INSTANCE.verbose("SDK initialized successfully.")// Warn log message
LoggerAnalytics.INSTANCE.verbose("Low memory warning detected.")// Error log message
LoggerAnalytics.INSTANCE.verbose("Failed to fetch user data: Network error.")
iOS (Swift)
// Verbose log messageLoggerAnalytics.verbose(log:"This is a verbose log message providing detailed internal information.")// Debug log messageLoggerAnalytics.debug(log:"Debugging info: API request started.")// Info log messageLoggerAnalytics.info(log:"SDK initialized successfully.")// Warn log messageLoggerAnalytics.warn(log:"Low memory warning detected.")// Error log messageLoggerAnalytics.error(log:"Failed to fetch user data: Network error.")
// Verbose log message
[RSSLoggerAnalyticsverbose:@"This is a verbose log message providing detailed internal information."];// Debug log message
[RSSLoggerAnalyticsdebug:@"Debugging info: API request started."];// Info log message
[RSSLoggerAnalyticsinfo:@"SDK initialized successfully."];// Warn log message
[RSSLoggerAnalyticswarn:@"Low memory warning detected."];// Error log message
[RSSLoggerAnalyticserror:@"Failed to fetch user data: Network error."error:nil];
Use a custom logger
You can create and use a custom logger that is used by the Kotlin and iOS (Swift) SDKs to log messages — this is helpful in cases where you don’t want to rely on the SDK’s default logger.
Android (Kotlin)
For the Android (Kotlin) SDK, you can inherit the Logger interface to create a custom Logger class and then pass its instance to Configuration while initializing the SDK.
For example, follow these steps to use the Timber library to create a Logger class:
For the iOS (Swift) SDK, you can inherit the Logger protocol to create a custom Logger class and then pass its instance to the setLogger API of LoggerAnalytics.
This site uses cookies to improve your experience while you navigate through the website. Out of
these
cookies, the cookies that are categorized as necessary are stored on your browser as they are as
essential
for the working of basic functionalities of the website. We also use third-party cookies that
help
us
analyze and understand how you use this website. These cookies will be stored in your browser
only
with
your
consent. You also have the option to opt-out of these cookies. But opting out of some of these
cookies
may
have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This
category only includes cookies that ensures basic functionalities and security
features of the website. These cookies do not store any personal information.
This site uses cookies to improve your experience. If you want to
learn more about cookies and why we use them, visit our cookie
policy. We'll assume you're ok with this, but you can opt-out if you wish Cookie Settings.