Android’s Logging

Sayed El-Abady
3 min readMar 14, 2020

--

Photo by Dan Edwards on Unsplash

Your manager stands in front of you asking you to solve that critical bug which causes your system to crash to some of the users, you try to reproduce what happened exactly with them with no luck and out of nowhere your teammate asks you: “Have you checked the logs?” then you have that huh moment and ask him “What?”, so let’s know first what’s logging.

What is Logging?

Logging -as Wikipedia says- is the cutting, skidding, on-site processing, and loading of trees or logs onto trucks. But that’s not what we mean with it in our software industry it means keeping records or information about your code what is it currently doing or what is the current data it works with.

That’s the concept itself or what does it mean, this concept is in almost all the programming languages there but it differs from one to another and we are going to talk about logging in Android.

Logging in Android

In the Android Development world, there’s a Log class which provides all the needed functionality for printing a local log message and it handles that through different types of method sand each method has a suitable situation for it, let’s discuss them:

  • Log.v: v stands for a Verbose. This is like a general type of a log message. You typically use if for logging every detail of something
  • Log.i: i stands for an Info. This is usually used when you want to log information like connecting to a socket, receiving a notification .. etc.
  • Log.d: d stands for Debug. This is usually used when you want to log some data while debugging something
  • Log.w: w stands for a Warning. This is usually used when something is not usual is happening but not necessarily an error.
  • Log.e: e stands for an Error. This is usually used to notify you that there’s something wrong is going on here (would be useful in the bug we are facing).
  • Log.wtf: wtf stands for ‘What a Terrible Failure’ not what you thought of it. It looks like Log.e, but it’s a way more dangerous. It’s to be used in these weird situations that shouldn’t be happening by any mean but they did.

You can see all the required logs from the logs section in the bottom bar in Android Studio:

All these logs will be available locally only and that’s something so useful in the development phase as you can see them while you are working or developing your feature but after the release, you will need to log them remotely somewhere and here come libraries like Timber, it’s an amazing library by Jake Wharton which lets you extend the logging by providing an implementation for logging depending on the build variant see timber-example for more information.

Notes on Logging:

Here are some notes that I think will help you will logging:

  • Logging may seem to you that it’s a good thing and we should log everything, it’s definitely a good and essential thing but you shouldn’t log everything because you can log some sensitive data which will put you in a trouble afterward, just log what will be helpful to you (Log.e and Log.w).
  • Don’t ever swallow exceptions, if you don’t know how to handle it right now and don’t want your app to crash because of them just Log them and you will definitely need these log someday, swallowing exceptions will lead to bugs that are so hard to debug.
  • Logging may decrease your code’s readability so make sure that you are doing in a well-structured way.

Thanks for reading! Be sure to clap below and recommend this article if you liked it.

Reach me out on Twitter, Linkedin.

--

--