Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Android Logging on Steroids: Clickable Logs With Location Info

 

 
Merthan Erdem
Android Developer at Deutsche Bahn, Student at HU Berlin
Published: July 23, 2020
Tweet
Share
 
 

Today I’ll quickly tell you a way to make every log in your application look like this with a clickable hyperlink to the location, just like with Exceptions:

 

D/(AddPictureFragment.kt:222)getImageCaptureId(): 3450803 

 

This was simply logged in the method getImageCaptureId() at line 222 of AddPictureFragment, and the call simply looked like this, no need to specify anything:

 

Timber.d("$id")

 

This often even takes away the need to define anything extra as even a “magic number” in the logs now gets lots of infos additionally added, worst case scenario when only specifying id would be that you have to click on the Class/Linenumber and see what was logged. Still miles better than the logging stuff I used before.

 

Additional Usages/Extensions

Not just that, but you can create methods like:

 

 

That allow you to just call .log() on any object and automatically gives you a useable log, to take the example from above with a variable named id you can just get a log like:

 

D/(AddPictureFragment.kt:222)getImageCaptureId(): 3450803

 

by doing

 

id.log()

 

Setup

First off, you’ll have to import the Timber library, so put this (check the current version first) into your app’s build.gradle

 

implementation 'com.jakewharton.timber:timber:4.7.1'

 

The only thing you need to do to setup Timber logging in general is “Planting” a Tree in your application class.

Instead of the default DebugTree we’ll use a class that extends from it and overrides createStackElementTag, which tells the Logger what the Tag (first part) of the Log should look like, which is also why we don’t need a variable for the actual log message.

 

 

After that the only thing we need to do is follow Timber’s setup guide, so call

 

Timber.plant(HyperlinkedDebugTree())

 

inside onCreate in your Application class and you are done.

You don’t even need to create a named class, you can also just use Kotlin’s anonymous class syntax and put the code into the Timber.plant call directly.

I created a Pull Request a while ago on the Timber library for this, as I think it makes sense to include something like this in the library itself (and if it’s included it should be mentioned in the Readme).

The PR also has a different Tree that let’s you pass whether or not you want to log the methodName, but that isn’t required as you can just customize your own HyperlinkedDebugTree here, feel free to choose a shorter name too :D.

Logging with Timber doesn’t happen in production applications so stuff like line-numbers/classes being logged won’t be a problem at all.

 

Thank you for reading, tell me if I can improve anything either in my Medium post or in the linked code. Have a great day!

 

 

 

Tags: Kotlin, Android App Development, Logging, AndroidDev

 

View original article at: 


 

Originally published: April 26, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One