A Tree is a handler for all the logs that you send through Forest logging
methods. This library comes with DebugTree, which logs the
log entry to the Android logcat. You can also implement your own Tree and plant it to a
Forest so you can process the logs.
Implement A Tree
In order to handle or process a log event, you must implement your own Tree. Here is an example of implementing a tree that send all the logs to Firebase Crashlytics:
// FirebaseCrashlyticsTree.kt
class FirebaseCrashlyticsTree : Tree {
override fun log(entry: LogEntry) {
val tag = if (!entry.tag.isNullOrEmpty()) {
"$tag - "
} else {
""
}
FirebaseCrashlytics.getInstance().log("${tag}${entry.message}")
}
}
Plant A Tree
You can plant your tree to Global Forest so the tree is available to all the Forests:
Forest.plant(FirebaseCrashlyticsTree())
Or if you prefer to plant to a particular Forest:
val forest = getForest(MyClass::class.java)
forest.plant(FirebaseCrashlyticsTree())
Note, a Tree will only receive a Tree.log call if
Forest.preProcessLog method return non-null value, and
Forest.level is higher than the logging level.