Napier
3.0.1indexedLogger library supports multiple platforms, displaying logs on each platform's native viewer. Features include customizable log levels, throwable logging, and integration with Crashlytics.
Logger library supports multiple platforms, displaying logs on each platform's native viewer. Features include customizable log levels, throwable logging, and integration with Crashlytics.

Napier is a logger library for Kotlin Multiplatform.
It supports Android, Darwin(iOS, macOS, watchOS, tvOS), JVM, JavaScript.
Logs written in common module are displayed on logger viewer of each platform.
format: [Class name]$[Method name]: [Your log]
uses the android.util.Log(Logcat)

format: [Date time][Symbol][Log level][Class name].[Method name] - [Your log]
Added [async] label at the end, if it is called from suspend functions.
uses the print

uses the console.log

uses the java.util.logging.Logger

You can download this library from MavenCentral or jCenter repository.
You can download this from 1.4.1.
Package name is io.github.aakira
repositories {
mavenCentral()
}
You can download this until 1.4.1.
Package name is com.github.aakira
repositories {
jCenter()
}
Set the version name in your build.gradle
def napierVersion = "[latest version]"
Add the dependency to your commonMain dependencies
sourceSets {
commonMain {
dependencies {
// ...
implementation "io.github.aakira:napier:$napierVersion"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.aakira:napier:$napierVersion")
}
}
}
// verbose log
Napier.v("Hello napier")
Napier.v { "Hello napier" }
// you can set a tag for each log
Napier.d("optional tag", tag = "your tag")
Napier.d(tag = "your tag") { "optional tag" }
try {
...
} catch (e: Exception) {
// you can set the throwable
Napier.e(, e)
Napier.e(e) { }
}
log { }
log(tag = ) { }
You must initialize the Napier in your module.
Napier.base(DebugAntilog())
fun debugBuild() {
Napier.base(DebugAntilog())
}
|argument|type|description| |-|-| |coroutinesSuffix|Boolean|Added [async] label at the end, if it is called from
suspend functions|
NapierProxyKt.debugBuild()
Napier.takeLogarithm()
You can use this library on the background thread on iOS using Kotlin.coroutines as native-mt.
internal val mainScope = SharedScope(Dispatchers.Main)
internal val backgroundScope = SharedScope(Dispatchers.Default)
internal class SharedScope(private val context: CoroutineContext) : CoroutineScope {
private job = Job()
exceptionHandler = CoroutineExceptionHandler { _, throwable ->
println()
}
coroutineContext: CoroutineContext
() = context + job + exceptionHandler
}
backgroundScope.launch {
suspendFunction()
}
You can inject custom Antilog.
So, you should change Antilogs in debug build or release build.
Crashlytics AntiLog samples
Sample projects use the Firebase Crashlytics.
You must set authentication files to android/google-services.json and ios/Napier/GoogleService-Info.plist.
Check the firebase document. [Android, iOS]
Write this in your application class.
if (BuildConfig.DEBUG) {
// Debug build
// disable firebase crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(false)
// init napier
Napier.base(DebugAntilog())
} else {
// Others(Release build)
// enable firebase crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true)
// init napier
Napier.base(CrashlyticsAntilog(this))
}
Write this in your AppDelegate.
#if DEBUG
// Debug build
// init napier
NapierProxyKt.debugBuild()
#else
// Others(Release build)
// init firebase crashlytics
.configure()
.releaseBuild(antilog: (
crashlyticsAddLog: { priority, tag, message
.crashlytics().log()
},
crashlyticsSendLog: { throwable
.crashlytics().record(error: throwable)
}))
Copyright (C) 2019 A.Akira
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
This library is inspired by Timber.
I recommend using it if it supports kotlin multiplatform project.😜
Thanks for advice.
@horita-yuya,
@terachanple
class Sample {
fun hello(): String {
Napier.v("Hello napier")
Napier.d("optional tag", tag = "your tag")
return "Hello Napier"
}
suspend fun suspendHello(): String {
Napier.i("Hello")
delay(3000L)
Napier.w("Napier!")
return "Suspend Hello Napier"
}
fun handleError() {
try {
throw Exception("throw error")
} catch (e: Exception) {
Napier.e("Napier Error", e)
}
}
}
| Platform | Sample |
|---|
| VERBOSE | Napier.v() |
| DEBUG | Napier.d() |
| INFO | Napier.i() |
| WARNING | Napier.w() |
| ERROR | Napier.e() |
| ASSERT | Napier.wtf() |
Surfaced from shared tags and platforms — no rankings paid for.