{"id":847,"date":"2019-08-15T12:21:28","date_gmt":"2019-08-15T11:21:28","guid":{"rendered":"http:\/\/toptechytips.com\/?p=847"},"modified":"2020-06-04T06:27:57","modified_gmt":"2020-06-04T06:27:57","slug":"add-logging-in-android-studio","status":"publish","type":"post","link":"https:\/\/dotslaz.com\/add-logging-in-android-studio\/","title":{"rendered":"How to add logging in android studio with examples!"},"content":{"rendered":"
Android Logging is a must to know every developer we believe. In their development lifecycle, one has to hear \u201cCheck the logs\u201d at least for once from others. In fact, checking the logs saves lots of unexpected errors. But First of all, we need a clear concept on it; Logging, log messages, advantages, and disadvantages, etc. Let\u2019s try to find it out.<\/p>\n
Logging \u2014 the cutting, skidding, on-site processing and loading of trees onto trucks (taken from Wikipedia). OK, but this is not what we are looking for. What is logging in software development? What is the role of it?
\nLogging is a process; a process of printing information about what exactly the code is doing, which data we are working on etc. In software development, there are different languages with different syntax, there are different platforms, but the general idea of logging is the same for all.<\/p>\n
Developers must learn the right way to log. If the logging process is properly, then it can be a saver to detect bugs, performance issues, etc. It can also provide related important information on the software or technology we are using. But if the logging process is wrong, it can create overhead for no reason; i.e. some developers may print a thousand lines of log message without any need.<\/p>\n
There are several types of lodging options in the Android framework (which is built-in) where a dedicated special class named \u201cLog\u201d to provide the functionality of printing a log message. There are different types of a log messages. It can be simple information, it can be an error. A developer may want to get the log in debug mode and not at the release time. We are going to discuss on the right below, Log.i \u2014 it is used to log INFO type messages. When someone wants log useful information; i.e. a file is downloading, a process is finished, connect to the cloud, etc, this type of log messages can be used.<\/p>\n
package com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n text_view_click.text = (viewModel?.count).toString()\r\n \/\/ Here we are logging the count\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n }\r\n }\r\n}\r\n<\/pre>\nLog.e \u2014 here e stands for error. Usually, this type of log message is used to print error details.<\/p>\n
This log messages often appear at the catch statements of our code.<\/h3>\n
package com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.text.TextUtils\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nimport kotlin.random.Random\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n val randomValue = Random.nextInt(0, 9)\r\n try {\r\n viewModel?.count = viewModel?.count!! \/ randomValue\r\n text_view_click.text = (viewModel?.count).toString()\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n } catch (e: Exception) {\r\n if (!TextUtils.isEmpty(e.message)) {\r\n \/\/ Here is the error log message\r\n Log.e(LOG_TAG, e.message!!)\r\n }\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\nLog.w \u2014 w stands for warning. The log message is of WARN type. This type implies that something
\nsomething strange happened, but it is not necessarily an error.<\/p>\npackage com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.text.TextUtils\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nimport kotlin.random.Random\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n val randomValue = Random.nextInt(0, 9)\r\n \/\/ Here is the warning log message\r\n Log.w(LOG_TAG, \"The value is: $randomValue. (It should not be zero)\")\r\n try {\r\n viewModel?.count = viewModel?.count!! \/ randomValue\r\n text_view_click.text = (viewModel?.count).toString()\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n } catch (e: Exception) {\r\n if (!TextUtils.isEmpty(e.message)) {\r\n \/\/ Here is the error log message\r\n Log.e(LOG_TAG, e.message!!)\r\n }\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\nLog.d \u2014 Sometimes developers might need to check if some of the codes\/ code blocks are properly
\nworking, the statements are running properly. This means the process is still under development and the
\ndeveloper wants to check something for now only. It\u2019s the time when this log message comes. d stands
\nfor DEBUG. Some may think that this log message is used for debugging, then all of the log messages will
\nbe removed automatically during runtime. And if you follow up on the official Android documentation, you
\nare right! But, in reality, that\u2019s not the case. It needs some additional steps in order to achieve that.<\/p>\npackage com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.text.TextUtils\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nimport kotlin.random.Random\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n \/\/ Here is the debug log message\r\n Log.w(LOG_TAG, \"The value is: $Random.nextInt(0, 9). Is it between 0 and 9?\")\r\n val randomValue = Random.nextInt(0, 9)\r\n \/\/ Here is the warning log message\r\n Log.w(LOG_TAG, \"The value is: $randomValue. (It should not be zero)\")\r\n try {\r\n viewModel?.count = viewModel?.count!! \/ randomValue\r\n text_view_click.text = (viewModel?.count).toString()\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n } catch (e: Exception) {\r\n if (!TextUtils.isEmpty(e.message)) {\r\n \/\/ Here is the error log message\r\n Log.e(LOG_TAG, e.message!!)\r\n }\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\nLog.v \u2014 v stands for VERBOSE. This is more like a general type of a log message. Developers can use it
\nwhenever you want to print some very detailed info about the event that occurred.<\/p>\npackage com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.text.TextUtils\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nimport kotlin.random.Random\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n \/\/ Here is the debug log message\r\n Log.w(LOG_TAG, \"The value is: $Random.nextInt(0, 9). Is it between 0 and 9?\")\r\n val randomValue = Random.nextInt(0, 9)\r\n \/\/ Here is the warning log message\r\n Log.w(LOG_TAG, \"The value is: $randomValue. (It should not be zero)\")\r\n try {\r\n viewModel?.count = viewModel?.count!! \/ randomValue\r\n text_view_click.text = (viewModel?.count).toString()\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n } catch (e: Exception) {\r\n if (!TextUtils.isEmpty(e.message)) {\r\n \/\/ Here is the error log message\r\n Log.e(LOG_TAG, e.message!!)\r\n }\r\n \/\/ Here is the full log message of the error\r\n Log.v(LOG_TAG, e.toString())\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\nLog.wtf \u2014 wtf stands for \u2018What a Terrible Failure\u2019. It looks like Log.e, but its way more sever. It\u2019s to be
\nused in these strange cases, which should never happen but they do from time to time.<\/p>\npackage com.example.architecturecomponents.ui.main\r\nimport android.os.Bundle\r\nimport android.text.TextUtils\r\nimport android.util.Log\r\nimport androidx.appcompat.app.AppCompatActivity\r\nimport androidx.lifecycle.ViewModelProviders\r\nimport com.example.architecturecomponents.R\r\nimport com.example.architecturecomponents.data.ClickLoggingInterceptor\r\nimport com.example.architecturecomponents.data.LoggingViewModel\r\nimport com.example.architecturecomponents.data.LoggingViewModelFactory\r\nimport com.example.architecturecomponents.ui.setRipple\r\nimport kotlinx.android.synthetic.main.activity_main.*\r\nimport kotlin.random.Random\r\nclass MainActivity : AppCompatActivity() {\r\n private var viewModel: LoggingViewModel? = null\r\n private val LOG_TAG = this.javaClass.simpleName\r\n override fun onCreate(savedInstanceState: Bundle?) {\r\n super.onCreate(savedInstanceState)\r\n setContentView(R.layout.activity_main)\r\n viewModel = ViewModelProviders.of(this,\r\nLoggingViewModelFactory(ClickLoggingInterceptor()))\r\n .get(LoggingViewModel::class.java)\r\n text_view_click.text = viewModel?.count.toString()\r\n text_view_click.setRipple(R.color.colorPrimary50)\r\n text_view_click.setOnClickListener {\r\n viewModel?.count = viewModel?.count?.inc()!!\r\n \/\/ Here is the debug log message\r\n Log.w(LOG_TAG, \"The value is: $Random.nextInt(0, 9). Is it between 0 and 9?\")\r\n val randomValue = Random.nextInt(0, 9)\r\n \/\/ Here is the warning log message\r\n Log.w(LOG_TAG, \"The value is: $randomValue. (It should not be zero)\")\r\n try {\r\n viewModel?.count = viewModel?.count!! \/ randomValue\r\n text_view_click.text = (viewModel?.count).toString()\r\n Log.i(LOG_TAG, (viewModel?.count).toString())\r\n } catch (e: Exception) {\r\n if (!TextUtils.isEmpty(e.message)) {\r\n \/\/ Here is the error log message\r\n Log.e(LOG_TAG, e.message!!)\r\n }\r\n \/\/ Here is the full log message of the error\r\n Log.v(LOG_TAG, e.toString())\r\n \/\/ Here is the log message of the error as WTF\r\n Log.wtf(LOG_TAG, \"What a terrible failure\", e)\r\n }\r\n }\r\n }\r\n}\r\n<\/pre>\nConclusion<\/h4>\n
Logging is a must and error-cutting process in software development. In the next episode, we will go through the usage of Timber to log in to Android. Stay with us.<\/p>\n","protected":false},"excerpt":{"rendered":"
Android Logging is a must to know every developer we believe. In their development lifecycle, one has to hear \u201cCheck the logs\u201d at least for once from others. In fact, checking the logs saves lots of unexpected errors. But First of all, we need a clear concept on it; Logging, log messages, advantages, and disadvantages, […]<\/p>\n","protected":false},"author":2,"featured_media":872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45,46,47],"tags":[],"yoast_head":"\n
How to add logging in android studio with examples! -Technology Blog<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\t\n\t\n\t\n