Send logs to Datadog from your iOS applications with Datadog’s
dd-sdk-ios client-side logging library and leverage the following features:
dd-sdk-ios library supports all iOS versions 11+.
Declare the library as a dependency depending on your package manager:
You can use CocoaPods to install
To integrate using Apple’s Swift Package Manager, add the following as a dependency to your
.package(url: "https://github.com/Datadog/dd-sdk-ios.git", .upToNextMajor(from: "1.0.0"))
You can use Carthage to install
Initialize the library with your application context and your Datadog client token. For security reasons, you must use a client token: you cannot use Datadog API keys to configure the
dd-sdk-ios library as they would be exposed client-side in the iOS application IPA byte code. For more information about setting up a client token, see the client token documentation:
Datadog.initialize( appContext: .init(), trackingConsent: trackingConsent, configuration: Datadog.Configuration .builderUsing(clientToken: "<client_token>", environment: "<environment_name>") .set(serviceName: "app-name") .build() )
Datadog.initialize( appContext: .init(), trackingConsent: trackingConsent, configuration: Datadog.Configuration .builderUsing(clientToken: "<client_token>", environment: "<environment_name>") .set(serviceName: "app-name") .set(endpoint: .eu) .build() )
To be compliant with the GDPR regulation, the SDK requires the
trackingConsent value at initialization.
trackingConsent can be one of the following values:
.pending- the SDK starts collecting and batching the data but does not send it to Datadog. The SDK waits for the new tracking consent value to decide what to do with the batched data.
.granted- the SDK starts collecting the data and sends it to Datadog.
.notGranted- the SDK does not collect any data: logs, traces, and RUM events are not sent to Datadog.
To change the tracking consent value after the SDK is initialized, use the
Datadog.set(trackingConsent:) API call.
The SDK changes its behavior according to the new value. For example, if the current tracking consent is
.granted, the SDK will send all current and future data to Datadog;
.notGranted, the SDK will wipe all current data and will not collect any future data.
When writing your application, you can enable development logs. All internal messages in the SDK with a priority equal to or higher than the provided level are then logged to console logs.
Datadog.verbosityLevel = .debug
logger = Logger.builder .sendNetworkInfo(true) .printLogsToConsole(true, usingFormat: .shortWith(prefix: "[iOS App] ")) .build()
Send a custom log entry directly to Datadog with one of the following methods:
logger.debug("A debug message.") logger.info("Some relevant information?") logger.notice("Have you noticed?") logger.warn("An important warning…") logger.error("An error was met!") logger.critical("Something critical happened!")
(Optional) - Provide a map of
attributes alongside your log message to add attributes to the emitted log. Each entry of the map is added as an attribute.
logger.info("Clicked OK", attributes: ["context": "onboarding flow"])
The following methods in
Logger.Builder can be used when initializing the logger to send logs to Datadog:
|Set to |
|Set to |
|Build a new logger instance with all options set.|
Find below methods to add/remove tags and attributes to all logs sent by a given logger.
addTag(withKey:value:) method to add tags to all logs sent by a specific logger:
// This adds a tag "build_configuration:debug" logger.addTag(withKey: "build_configuration", value: "debug")
<TAG_VALUE> must be a
removeTag(withKey:) method to remove tags from all logs sent by a specific logger:
// This removes any tag starting with "build_configuration" logger.removeTag(withKey: "build_configuration")
By default, the following attributes are added to all logs sent by a logger:
http.useragentand its extracted
network.client.ipand its extracted geographical properties (
logger.version, Datadog SDK version
version, client’s app version extracted from
environment, the environment name used to initialize the SDK
addAttribute(forKey:value:) method to add a custom attribute to all logs sent by a specific logger:
// This adds an attribute "device-model" with a string value logger.addAttribute(forKey: "device-model", value: UIDevice.current.model)
<ATTRIBUTE_VALUE> can be anything conforming to
Codable data model, …).
removeAttribute(forKey:) method to remove a custom attribute from all logs sent by a specific logger:
// This removes the attribute "device-model" from all further log send. logger.removeAttribute(forKey: "device-model")