Skip to content

Logging

You have two options: swift-log for full LogCat experience or classic print.

Using swift-log

Add the following dependencies to your Package.swift:

.package(url: "https://github.com/swifdroid/AndroidLogging.git", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.6.2"),
// target
.product(name: "Logging", package: "swift-log"),
.product(name: "AndroidLogging", package: "AndroidLogging", condition: .when(platforms: [.android])),

Import the necessary modules at the top of your Swift file:

#if os(Android)
// Official Swift bindings for Android NDK
import Android
// Specific logging handler for Android platform
import AndroidLogging
#endif
// Lightweight and fast logging system
import Logging

Add the following code to your library's entry point (e.g., in the initialize function):

// Activate logger
LoggingSystem.bootstrap(AndroidLogHandler.taggedBySource)

This will let you use the swift-log API to log messages, which will be directed to LogCat.

let logger = Logger(label: "🐦‍🔥 SWIFT")
logger.info("🚀 Hello World!")

Using print

Built-in print function doesn't work out of the box on Android.
To use it, you have to adopt PrintRedirector.

Info

It was originally made by Sergey Romanenko → here

It is the part of Droid package which is for Android GUI applications, but not a part of JNIKit library, so you have to copy this code into your library project manually.

Once you → copied it from the Droid package add the following code to your library's entry point (e.g., in the initialize function):

// Enable `print` redirect into LogCat
#if os(Android)
try? PrintRedirector.shared.redirectPrint()
#endif

And then feel free to use print anywhere in your code:

print("Hello from Swift for Android!")

Note

It will always print to LogCat with the Swift tag and the INFO log level.