Napier adalah perpustakaan logger untuk Kotlin Multiplatform.
Mendukung Android, Darwin (iOS, macOS, watchOS, tvOS), JVM, JavaScript.
Log yang ditulis dalam modul umum ditampilkan pada penampil logger setiap platform.
format: [Class name]$[Method name]: [Your log]
menggunakan android.util.Log
(Logcat)
format: [Date time][Symbol][Log level][Class name].[Method name] - [Your log]
Menambahkan label [async]
di akhir, jika dipanggil dari fungsi penangguhan.
menggunakan print
tersebut
menggunakan console.log
menggunakan java.util.logging.Logger
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)
}
}
}
Anda dapat mengunduh perpustakaan ini dari repositori MavenCentral atau jCenter.
Anda dapat mengunduh ini dari 1.4.1
.
Nama paketnya adalah io.github.aakira
repositories {
mavenCentral()
}
Anda dapat mengunduh ini hingga 1.4.1
.
Nama paketnya adalah com.github.aakira
repositories {
jCenter()
}
Tetapkan nama versi di build.gradle Anda
def napierVersion = "[latest version]"
Tambahkan ketergantungan ke dependensi commonMain Anda
sourceSets {
commonMain {
dependencies {
// ...
implementation " io.github.aakira:napier: $n apierVersion "
}
}
}
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( " Napier Error " , e)
Napier .e(e) { " Napier Error " }
}
// you can also use top-level function
log { " top-level " }
log(tag = " your tag " ) { " top-level " }
Anda harus menginisialisasi Napier di modul Anda.
Napier .base( DebugAntilog ())
fun debugBuild () {
Napier .base( DebugAntilog ())
}
|argumen|jenis|deskripsi| |-|-| |coroutinesSuffix|Boolean|Menambahkan label [async]
di akhir, jika dipanggil dari fungsi penangguhan|
NapierProxyKt . debugBuild ( )
Napier .takeLogarithm()
Platform | Mencicipi |
---|---|
VERBOSE | Napier.v() |
DEBUG | Napier.d() |
INFORMASI | Napier.i() |
PERINGATAN | Napier.w() |
KESALAHAN | Napier.e() |
MENEGASKAN | Napier.wtf() |
Anda dapat menggunakan perpustakaan ini di thread latar belakang di iOS menggunakan Kotlin.coroutines sebagai native-mt.
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
}
backgroundScope.launch {
suspendFunction()
}
Anda dapat menyuntikkan Antilog
khusus.
Jadi, Anda harus mengubah Antilogs di debug build atau rilis build.
Sampel AntiLog Crashlytics
Contoh proyek menggunakan Firebase Crashlytics.
Anda harus menyetel file autentikasi ke android/google-services.json
dan ios/Napier/GoogleService-Info.plist
.
Periksa dokumen firebase. [Android, iOS]
Tulis ini di kelas aplikasi Anda.
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 ))
}
Tulis ini di AppDelegate Anda.
#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
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.
Perpustakaan ini terinspirasi oleh Timber.
Saya sarankan menggunakannya jika mendukung proyek multiplatform kotlin.?
Terima kasih atas sarannya.
@horita-yuya, @terachanple