• Stars
    star
    587
  • Rank 76,145 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Logging library for Kotlin Multiplatform

logo

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.

Preview

Android

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

uses the android.util.Log(Logcat)

preview-android

Darwin(iOS, macOS, watchOS, tvOS)[Intel/Apple silicon]

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

preview-ios

JavaScript

uses the console.log

preview-js

JVM

uses the java.util.logging.Logger

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")

        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 MavenCentral or jCenter repository.

  • Maven central

You can download this from 1.4.1.
Package name is io.github.aakira

repositories {
    mavenCentral()
}
  • jCenter

You can download this until 1.4.1.
Package name is com.github.aakira

repositories {
    jCenter()
}

Version

Set the version name in your build.gradle

Maven Central

def napierVersion = "[latest version]"

Common

Add the dependency to your commonMain dependencies

  • groovy
sourceSets {
    commonMain {
        dependencies {
            // ...
            implementation "io.github.aakira:napier:$napierVersion"
        }
    }
}
  • kts
sourceSets {
    val commonMain by getting {
        dependencies {
            implementation("io.github.aakira:napier:$napierVersion")
        }
    }
}

Usage

How to use

Common module

// 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("Napier Error", e)
    Napier.e(e) { "Napier Error" }
}

// you can also use top-level function
log { "top-level" }
log(tag = "your tag") { "top-level" }

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())
}

|argument|type|description| |-|-| |coroutinesSuffix|Boolean|Added [async] label at the end, if it is called from suspend functions|

  • Call initialize code from ios project.
NapierProxyKt.debugBuild()

Clear antilog

Napier.takeLogarithm()

Log level

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

Run background thread

You can use this library on the background thread on iOS using Kotlin.coroutines as native-mt.

  • Define scope
internal val mainScope = SharedScope(Dispatchers.Main)

internal val backgroundScope = SharedScope(Dispatchers.Default)

internal class SharedScope(private val context: CoroutineContext) : CoroutineScope {
    private val job = Job()
    private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
        println("[Coroutine Exception] $throwable")
    }

    override val coroutineContext: CoroutineContext
        get() = context + job + exceptionHandler
}
  • Usage
backgroundScope.launch {
    suspendFunction()
}

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

    // 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
FirebaseApp.configure()

// init napier
NapierProxyKt.releaseBuild(antilog: CrashlyticsAntilog(
    crashlyticsAddLog: { priority, tag, message in
        Crashlytics.crashlytics().log("\(String(describing: tag)): \(String(describing: message))")
},
    crashlyticsSendLog: { throwable in
        Crashlytics.crashlytics().record(error: throwable)
}))
#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

https://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 using it if it supports kotlin multiplatform project.😜

Thanks for advice.
@horita-yuya, @terachanple

More Repositories

1

Kotlin-Multiplatform-Libraries

Kotlin Multiplatform Libraries. Welcome PR if you find or create new Kotlin Multiplatform Library.
2,040
star
2

ExpandableLayout

[Deprecated] An android library that brings the expandable layout with various animation. You can include optional contents and use everywhere.
Java
1,656
star
3

CompoundIconTextView

An android library that is able to set a vector drawable at text view pre-Lollipop.
Java
247
star
4

OkWear

Easily connection between Android wear and Handheld (Mobile device)
Java
124
star
5

ExoPlayerManager

An android library that wraps the ExoPlayer and the IMA Android SDK which plays a video advertisement. This is written in Kotlin.
Kotlin
78
star
6

dagger-hilt-example

This repository is an example of the dagger android hilt plugin using an android view model
Kotlin
59
star
7

mpp-example

This project is a minimum example of Kotlin Multiplatform Project.
Kotlin
32
star
8

DaggerInstantApps

Instant apps sample using dagger2 android support.
Kotlin
32
star
9

flutter-graphql-example

Dart
19
star
10

KotlinNativeSample

KotlinNativeSampleProject, Android, iOS
Swift
16
star
11

multithread-mpp

This is the best architecture of Kotlin Multiplatform Project I think! This project works on background thread using kotlinx.Coroutines.
Kotlin
16
star
12

KotlinMultiplatformAndoridParcelize

Use the Parcelize Annotation of the Kotlin Android Extensions in Kotin Multiplatform projects
Swift
15
star
13

Java2KotlinPractice

Java
13
star
14

flutter-parallax-walkthrough

Dart
5
star
15

git_info_plus

Get git information from within the Flutter application
Dart
4
star
16

MaterialColorPaletteTemplate

This is values of color for Android
4
star
17

gmail-to-calendar

JavaScript
3
star
18

flutter-hierarchical-transitions

This is an example of hierarchical transitions on the flutter app.
Dart
1
star
19

Vim-Commands

convenience commands
1
star
20

dotfiles

my vimrc etc...
Shell
1
star
21

aakira

1
star