logger README

项目的 README,来自 github.com/orhanobut/logger。合并为一个 markdown 文件:README.md。

Logger

Logger logo

Readable, structured logs for Android. Turn messages, collections, JSON, and XML into output that's easy to scan in Logcat.

Example Logcat output

Setup

Add Google Maven and Maven Central to your dependency repositories:

dependencyResolutionManagement {
  repositories {
    google()
    mavenCentral()
  }
}
dependencies {
  implementation("com.orhanobut:logger:2.2.0")
}

Initialize once in your application, then log:

Logger.addLogAdapter(AndroidLogAdapter())
Logger.d("hello %s", "world")

Messages

Logger.d("debug")
Logger.e("error")
Logger.w("warning")
Logger.v("verbose")
Logger.i("information")
Logger.wtf("What a Terrible Failure")

Logger.d("hello %s", "world")
Logger.d(listOf("foo", "bar"))
Logger.d(mapOf("key" to "value"))
Logger.d(intArrayOf(1, 2, 3))
Logger.json("""{"items":[1,2,3]}""")
Logger.xml("<root><item>value</item></root>")

Collection and structured output use debug priority. Include an exception and its cause with Logger.e(throwable, "message").

Set a tag for exactly one message on the calling thread:

Logger.t("Checkout").d("Order submitted")

Formatting and filtering

val format = PrettyFormatStrategy.newBuilder()
  .showThreadInfo(false) // Default: true
  .methodCount(0)        // Default: 2 caller frames
  .methodOffset(0)       // Default: 0 additional frames skipped
  .tag("MyApp")          // Default: PRETTY_LOGGER
  .build()

Logger.addLogAdapter(object : AndroidLogAdapter(format) {
  override fun isLoggable(priority: Int, tag: String?): Boolean = BuildConfig.DEBUG
})

Logger.t("Screen").d("A message tagged MyApp-Screen")

Override LogAdapter.isLoggable to filter messages by priority or tag. The example above enables logging only in debug builds. Use Logger.clearLogAdapters() before replacing the configured adapters.

For Logcat filtering, use PRETTY_LOGGER or your configured tag.

File logging

Save formatted logs to app-private storage with CsvFormatStrategy and a custom DiskLogStrategy. The sample shows the complete setup, including background writes and cleanup. App-private storage requires no storage permission.

The default DiskLogAdapter() still uses the legacy shared external-storage location. Modern Android restricts that location through scoped storage; use the sample's custom strategy for current applications.

Timber integration

Timber.plant(object : Timber.DebugTree() {
  override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
    Logger.log(priority, tag, message, t)
  }
})

License

Apache License 2.0

logger (orhanobut/logger)

正在搜索数据库…

13.8K ·GitHub
这是什么?Logger 是一个简单、美观且功能强大的 Android 日志库,能让 Logcat 的输出更易读。
这是独立应用,还是需要添加到 Android 项目中的库?它是一个 Android/Java/Kotlin 库,你需要将其作为依赖引入,并在应用代码中初始化。它本身没有界面,也不是独立应用。
我该如何把它添加到我的 Android 项目中?下载该库(将其添加为依赖),在应用中初始化一次 Logger,然后在需要格式化输出的地方调用其日志方法。
Logger 的输出与普通的 Android Log 调用有何不同?根据其 GitHub 主题('pretty-printer'、'json-logging'),Logger 会用带结构的边框格式化输出,还能美化打印 JSON,使日志在 Logcat 中比原始的 Log.d 输出更易浏览。
它和 Jake Wharton 的 Timber 库类似吗?该仓库将 'timber' 列为相关主题,这表明 Logger 扮演着类似的角色,即对 Android 的 Log 类进行即插即用的封装,但更侧重于美化输出和 JSON 格式化输出。
它同时适用于 Java 和 Kotlin 的 Android 项目吗?适用。该库本身使用 Kotlin 编写,其主题同时包含 'java' 和 'kotlin',因此用这两种语言编写的 Android 应用都可以使用它。
作者
orhanobut
大小
1.9 MB
星标
13.8K
时长
11年
提交 · 90 天