Clickable Line Numbers in Timber Logs: Boosting Your Android Debugging Experience

Android|Apr 16, 2023|Last edited: May 4, 2023
  • Timber
  • Logging
  • Android Studio
  • Clickable Line Numbers in Timber Logs: Boosting Your Android Debugging Experience
    Apr 16, 2023
    Improve Android logging with a custom debug tree adding clickable line numbers to Timber log messages. Follow our guide for better code navigation and debugging.
    Android Studio

    Enhance Your Android Logging with Clickable Line Numbers using Timber

    As an Android developer, you're likely familiar with how crucial logging is for debugging and maintaining your applications. One popular logging library is Timber, created by Jake Wharton. While Timber is powerful and easy to use, there's always room for improvement. In this blog post, we'll show you how to create a custom debug tree that adds clickable line numbers to your Timber log messages, making it even easier to navigate your code and identify issues.

    Timber: A Brief Overview

    Timber is an extensible logging library for Android that provides utility on top of Android's native Log class. It simplifies logging by allowing you to plant various "trees" that determine how logs are processed. You can learn more about Timber and its features on its GitHub repository:

    Step 1: Create a Custom Debug Tree

    To start, create a new file named ClickableLineNumberDebugTree.kt and add the following code:
    package com.example.bettertimberlogsandroid import timber.log.Timber class ClickableLineNumberDebugTree : Timber.DebugTree() { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { findLogCallStackTraceElement()?.let { element -> val lineNumberInfo = "(${element.fileName}:${element.lineNumber})" val updatedMessage = "$lineNumberInfo: $message" super.log(priority, tag, updatedMessage, t) } ?: run { super.log(priority, tag, message, t) } } override fun createStackElementTag(element: StackTraceElement): String? { return element.className } private fun findLogCallStackTraceElement(): StackTraceElement? { val stackTrace = Throwable().stackTrace var foundDebugTree = false return stackTrace.firstOrNull { element -> if (element.className.contains("ClickableLineNumberDebugTree")) { foundDebugTree = true false } else { foundDebugTree && !element.className.contains("Timber") } } } }
    This custom debug tree extends Timber's DebugTree class and overrides the log method. It first locates the stack trace element corresponding to the log call and then adds the file name and line number to the message. This makes the line number clickable in the Logcat output.

    Making it clickable

    val lineNumberInfo = "(${element.fileName}:${element.lineNumber})”
    To make the line number clickable in Android Studio's logcat, the log message format should include the file name and line number in a specific format: filename.extension:line_number.

    Step 2: Plant the Custom Debug Tree

    In your Application class, for example, App.kt, plant the custom debug tree:
    package com.example.bettertimberlogsandroid import import timber.log.Timber class App : Application() { override fun onCreate() { super.onCreate() Timber.plant(ClickableLineNumberDebugTree()) Timber.d("Hello Timber") } }

    Step 3: Use Timber in Your Activities

    Now, you can use Timber in your activities, like MainActivity.kt, to log messages:
    class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Timber.d("Hello MainActivity") setContent { BetterTimberLogsAndroidTheme { // A surface container using the 'background' color from the theme Surface( modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { Greeting("Android") } } } } }
    When you log a message using Timber, you'll see the log message with the line number in Logcat. Clicking the line number will redirect you to the file and the specific line number in your code.

    Demo of Clickable Line Numbers in Timber



    By extending Timber's functionality with a custom debug tree, you can make your Android development experience even better. With clickable line numbers in your log messages, navigating your code and identifying issues becomes a breeze


    Reveal: A Jetpack Compose Library for Engaging Reveal EffectsEnhancing Android Logging: Add JSON Formatting to Timber with Moshi