From c61a7b64153045276889cca6d7baaf9dd175f6b4 Mon Sep 17 00:00:00 2001 From: Drew Hamilton Date: Tue, 19 Mar 2019 16:30:41 +0100 Subject: [PATCH 1/2] Add and test `DebugTree#ignoreForTagging` --- timber/src/main/java/timber/log/Timber.kt | 10 +++++++++- timber/src/test/java/timber/log/TimberTest.kt | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/timber/src/main/java/timber/log/Timber.kt b/timber/src/main/java/timber/log/Timber.kt index 62b6a0759..a17e16a9a 100644 --- a/timber/src/main/java/timber/log/Timber.kt +++ b/timber/src/main/java/timber/log/Timber.kt @@ -194,7 +194,7 @@ class Timber private constructor() { /** A [Tree] for debug builds. Automatically infers the tag from the calling class. */ open class DebugTree : Tree() { - private val fqcnIgnore = listOf( + private val fqcnIgnore = mutableSetOf( Timber::class.java.name, Timber.Forest::class.java.name, Tree::class.java.name, @@ -206,6 +206,14 @@ class Timber private constructor() { .first { it.className !in fqcnIgnore } .let(::createStackElementTag) + /** + * Register an extra class name to be ignored when inferring the log tag from the + * stacktrace. + */ + fun ignoreForTagging(ignoredClass: Class) { + fqcnIgnore.add(ignoredClass.name) + } + /** * Extract the tag which should be used for the message from the `element`. By default * this will use the class name without any anonymous class suffixes (e.g., `Foo$1` diff --git a/timber/src/test/java/timber/log/TimberTest.kt b/timber/src/test/java/timber/log/TimberTest.kt index 126c50dc9..9f79fed12 100644 --- a/timber/src/test/java/timber/log/TimberTest.kt +++ b/timber/src/test/java/timber/log/TimberTest.kt @@ -209,6 +209,18 @@ class TimberTest { .hasNoMoreMessages() } + @Test fun debugTreeDoesNotUseIgnoredClassForTag() { + val tree = Timber.DebugTree() + tree.ignoreForTagging(ThisIsAReallyLongClassName::class.java) + Timber.plant(tree) + + ThisIsAReallyLongClassName().run() + + assertLog() + .hasDebugMessage("TimberTest", "Hello, world!") + .hasNoMoreMessages() + } + @Test fun debugTreeCustomTag() { Timber.plant(Timber.DebugTree()) Timber.tag("Custom").d("Hello, world!") From a3decdcac56c8766e423bf8bddbff201eab7b79c Mon Sep 17 00:00:00 2001 From: Drew Hamilton Date: Fri, 3 Jul 2020 08:47:56 +0200 Subject: [PATCH 2/2] Convert custom ignore list to constructor parameter --- timber/src/main/java/timber/log/Timber.kt | 14 +++----------- timber/src/test/java/timber/log/TimberTest.kt | 3 +-- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/timber/src/main/java/timber/log/Timber.kt b/timber/src/main/java/timber/log/Timber.kt index a17e16a9a..8f6e0f13b 100644 --- a/timber/src/main/java/timber/log/Timber.kt +++ b/timber/src/main/java/timber/log/Timber.kt @@ -193,27 +193,19 @@ class Timber private constructor() { } /** A [Tree] for debug builds. Automatically infers the tag from the calling class. */ - open class DebugTree : Tree() { - private val fqcnIgnore = mutableSetOf( + open class DebugTree(vararg ignoredClasses: Class<*>) : Tree() { + private val fqcnIgnore = setOf( Timber::class.java.name, Timber.Forest::class.java.name, Tree::class.java.name, DebugTree::class.java.name - ) + ).plus(ignoredClasses.map { it.name }) override val tag: String? get() = super.tag ?: Throwable().stackTrace .first { it.className !in fqcnIgnore } .let(::createStackElementTag) - /** - * Register an extra class name to be ignored when inferring the log tag from the - * stacktrace. - */ - fun ignoreForTagging(ignoredClass: Class) { - fqcnIgnore.add(ignoredClass.name) - } - /** * Extract the tag which should be used for the message from the `element`. By default * this will use the class name without any anonymous class suffixes (e.g., `Foo$1` diff --git a/timber/src/test/java/timber/log/TimberTest.kt b/timber/src/test/java/timber/log/TimberTest.kt index 9f79fed12..37ffc598a 100644 --- a/timber/src/test/java/timber/log/TimberTest.kt +++ b/timber/src/test/java/timber/log/TimberTest.kt @@ -210,8 +210,7 @@ class TimberTest { } @Test fun debugTreeDoesNotUseIgnoredClassForTag() { - val tree = Timber.DebugTree() - tree.ignoreForTagging(ThisIsAReallyLongClassName::class.java) + val tree = Timber.DebugTree(ThisIsAReallyLongClassName::class.java) Timber.plant(tree) ThisIsAReallyLongClassName().run()