Android Logging API Introduction and implementation of popular Logging Library - Timber

Timber is a logging library used in Android




Android's Logging API

This logging API will let me output short messages to the console so that I can view them and see the result. 
Log.i(tag= "MainActivity", msg= "onCreate called")

Logs have different levels which are used in different situations. The levels and their usages are listed below:

  • Verbose: Show all log messages (the default).
  • Debug: Show debug log messages that are useful during development only, as well as the message levels lower in this list.
  • Info: Show expected log messages for regular usage, as well as the message levels lower in this list.
  • Warn: Show possible issues that are not yet errors, as well as the message levels lower in this list.
  • Error: Show issues that have caused errors, as well as the message level lower in this list.
  • Assert: Show issues that the developer expects should never happen.


Benefits of using Timber:
  • Generates Automatic Tags so no need to setup manual tags 
  • Avoid logs in released app apks 
  • Easy Integration with crash reporting libraries 
Setup Timber
  1. Add Timber to Gradle 
  2. Make Application Class 
  3. Add Application to Manifest 
  4. Initialise Timber in Application Class 

implementation "com.jakewharton.timber:timber:$timberVersion"


timberVersion = '4.7.1'


Application Class
  • A base class that contains global application state for your entire app. 
  • It's also the main object that the OS uses to interact with your app. 
  • There is a default application class that is used by OS if you don't do anything special. So there is always going to be an application object that's out there created for your app. 
  • But sometimes you might want to do some specific setup for a utility that the entire app is going to use and Timber is a great example of this. 
  • Timber needs this application class because the whole app will be using this logging library. 

# Make sure you add it to the <application> tag in manifest file 
android:name= ".YourApplication"

// initalize timber
Timber.plant(Timber.DebugTree())
// using timber log 
Timber.i("onCreate Called ")



Comments

Popular Posts