在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):AAkira/Napier开源软件地址(OpenSource Url):https://github.com/AAkira/Napier开源编程语言(OpenSource Language):Kotlin 76.9%开源软件介绍(OpenSource Introduction):Napier is a logger library for Kotlin Multiplatform. PreviewAndroidformat: uses the Darwin(iOS, macOS, watchOS, tvOS)[Intel/Apple silicon]format: Added uses the JavaScriptuses the JVMuses the
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)
}
}
} DownloadRepositoryYou can download this library from MavenCentral or jCenter repository.
You can download this from repositories {
mavenCentral()
}
You can download this until repositories {
jCenter()
} VersionSet the version name in your build.gradle
CommonAdd 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")
}
}
} UsageHow to useCommon 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" }
InitializeYou must initialize the Napier in your module. AndroidNapier.base(DebugAntilog()) iOS
fun debugBuild() {
Napier.base(DebugAntilog())
} |argument|type|description| |-|-| |coroutinesSuffix|Boolean|Added
NapierProxyKt.debugBuild() Clear antilogNapier.takeLogarithm() Log level
Run background threadYou 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 val job = Job()
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
println("[Coroutine Exception] $throwable")
}
override val coroutineContext: CoroutineContext
get() = context + job + exceptionHandler
}
backgroundScope.launch {
suspendFunction()
} AdvancementYou can inject custom CrashlyticsCrashlytics AntiLog samples Sample projects use the Firebase Crashlytics. 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
CreditThis library is inspired by Timber. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论