Reputation: 1049
Timber is a great library for logging in Android. In Kotlin classes though, doesn't output anything. How can I fix this?
MainActivity.kt code:
Timber.e("Timber Log 1")
Log.e("MainActivity", "Log 1")
Gradle: I've tried the regular Java Timber:
implementation 'com.jakewharton.timber:timber:4.7.1'
And this Kotlin specific wrapper:
implementation 'com.github.ajalt:timberkt:1.5.1'
Same result. No output with either. Only from the Log.e()
Upvotes: 34
Views: 17873
Reputation: 43
Check Initialization: Ensure that you've properly initialized Timber in your application. It's usually done in the onCreate
method of your Application
class.
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}
}
Just make sure you are importing BuildConfig
from the correct location.
The correct way is: should be configured appropriately in your project's Gradle files
for example import com.example.appname.BuildConfig
.
If it still does not work, try import androidx.viewbinding.BuildConfig
Remember to make sure that your app's build configuration is set up correctly in your build.gradle
files to ensure that BuildConfig.DEBUG
behaves as expected. Debug and release configurations should be configured appropriately in your project's Gradle files.
Ensure that there is no space between the words in the tag. Otherwise, the tag you add to logcat will only display according to the first word. Briefly, space is not counted by logcat.
Upvotes: 1
Reputation: 815
Probably late to the party but my problem was the my phone was set to "Charge only" and not "file transfer". Apparently I was allowed to build and run, but logs were blocked
EDIT Another solution:
Check your RUN tab in the bottom of Android Studio. Sometimes the logs get output to there instead
Upvotes: 2
Reputation: 101
In my case it was wrong BuildConfig import
import org.koin.android.BuildConfig
but my app has
import com.company.example.BuildConfig
Upvotes: 5
Reputation: 1019
I have faced same problem, using Kotlin and Android studio 3.6 Follow these steps:
Add the following in build.gradle(Module: App)
implementation 'com.jakewharton.timber:timber:4.7.1'
Initialize Timber
in Application Class:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
if(BuildConfig.DEBUG){
Timber.plant(Timber.DebugTree())
}
}
}
Add the Application class(MyApp
) to the Manifest (AndroidManifest.xml)
<application
android:name=".MyApp"
Now you can use Timber: Timber.i("Timber logs")
Also can use custom tags if you wish: Timber.tag("Yo").I("used custom tag for logs")
Upvotes: 28
Reputation: 37404
The first step of Timber is to plant the tree as mentioned in docs
Behavior is added through Tree instances. You can install an instance by calling Timber.plant. Installation of Trees should be done as early as possible. The onCreate of your application is the most logical choice.
And use the debugTree
The
DebugTree implementation will automatically figure out from which
class it's being called and use that class name as its tag
. Since the tags vary
If you don't do this then you will have no logs entry and do this as soon as possible, like in oncreate
or better inside application class so do this
Timber.plant(Timber.DebugTree());
Upvotes: 38