Skip to content

Logging library for Kotlin Multiplatform

Notifications You must be signed in to change notification settings

rodionov-andrei/Napier

 
 

Repository files navigation

logo

Napier is a logger library for Kotlin Multiplatform.
It supports for the android, ios, jvm, js.
Logs written in common module are displayed on logger viewer of each platform.

  • Android

format: [Class name]$[Method name]: [Your log]

preview-android

  • ios

format: [Date time][Symbol][Log level][Class name].[Method name] - [Your log]

preview-ios

  • js

preview-js

  • jvm

preview-jvm

  • common sample code
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")

        runBlocking {
            delay(3000L)
        }

        Napier.w("Napier!")

        return "Suspend Hello Napier"
    }

    fun handleError() {
        try {
            throw Exception("throw error")
        } catch(e: Exception) {
            Napier.e("Napier Error", e)
        }
    }
}

Download

Repository

You can download this library from jCenter or maven repository.

  • jCenter
repositories {
    jCenter()
}
  • maven
repositories {
    maven { url "https://dl.bintray.com/aakira/maven" }
}

Version

Set the version name in your build.gradle

Download Napier

def napierVersion = "[latest version]"

Common

dependencies {
    implementation "com.github.aakira:napier:$napierVersion"
}

ios

uses the print

dependencies {
    implementation "com.github.aakira:napier-ios:$napierVersion"
}

android

uses the android.util.Log(Logcat)

dependencies {
    implementation "com.github.aakira:napier-android:$napierVersion"
}

js

uses the console.log

dependencies {
    implementation "com.github.aakira:napier-js:$napierVersion"
}

jvm

uses the java.util.logging.Logger

dependencies {
    implementation "com.github.aakira:napier-jvm:$napierVersion"
}

Usage

How to use

Common module

// verbose log
Napier.v("Hello napier")

// you can set a tag for each log
Napier.d("optional tag", tag = "your tag")

try {
    ...
} catch(e: Exception) {
    // you can set the throwable
    Napier.e("Napier Error", e)
}

Initialize

You must initialize the Napier in your module.

Android

Napier.base(DebugAntilog())

iOS

  • Write initialize code in your kotlin mpp project.
fun debugBuild() {
    Napier.base(DebugAntilog())
}
  • Call initialize code from ios project.
NapierProxyKt.debugBuild()

Log level

Platform Sample
VERBOSE Napier.v()
DEBUG Napier.d()
INFO Napier.i()
WARNING Napier.w()
ERROR Napier.e()
ASSERT Napier.wtf()

Advancement

You can inject custom Antilog.
So, you should change Antilogs in debug build or release build.

Crashlytics

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

    // init napier
    Napier.base(DebugAntilog())
} else {
    // Others(Release build)

    // init firebase crashlytics
    Fabric.with(this, Crashlytics())
    // 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
FirebaseApp.configure()
Fabric.with([Crashlytics.self])

// init napier
NapierProxyKt.releaseBuild(antilog: CrashlyticsAntilog(
    crashlyticsAddLog: { priority, tag, message in
        let args = [tag, message].compactMap { $0 }
        CLSLogv("%@", getVaList(args))
        return .init()
},
    crashlyticsSendLog: { throwable in
        Crashlytics.sharedInstance().recordError(throwable)
        return .init()
}))
#endif

License

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

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Credit

This library is inspired by Timber.
I recommend use it if it supports kotlin multiplatform project.😜

Thanks for advice.
@horita-yuya, @terachanple

About

Logging library for Kotlin Multiplatform

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 83.6%
  • Swift 7.4%
  • Ruby 7.2%
  • Other 1.8%