[Tutorial] ADB Logcat for capturing application logs.

10499

4

2016-05-25 15:01

Edited by shekhutanwar at 2016-05-25 15:34

Hello all,

This is my first article on Forum on How to use ADB logcat (a great tool for debugging and capturing the logs of any android application).

The first thing is what is ADB?
Well, ADB, Android Debug Bridge, is a command-line utility included with Google’s Android SDK. ADB can control your device over USB from a computer, copy files back and forth, install and uninstall apps, run shell commands, and more.

I will be focusing more on the adb logcat for taking logs to help forum users gain more knowledge of the testing and capturing logs. Remember i am talking here about capturing logs of android applications NOT the FlymeOS.

How ADB produces the log?
Every android developer when writing code for an android application, write a single line of code that writes the String to console. and through that console adb captures the logs.

The Log class allows you to create log entries in your code that display in the logcat tool. Common logging methods include:

Here is the Java Code:-
  1. Log.i("MyActivity", "This log is info log, and this will be printed on the console");
Copy the Code

and the command prompt or Console will print the log like this:-
  1. I/MyActivity( 1557): This log is info log, and this will be printed on the console
Copy the Code

This is how adb is able to capture the log of any android application. Log is very much helpful in debugging an android application when any application crashes or doesn't works as intended.


How do i start working on ADB logcat? What are the steps for tools installation and steps for enabling this feature?
The requirements of using ADB are as follows:-

1. A Windows OR Linux OR Mac PC OR any system that supports adb tool.

2. Appropriate ADB Drivers for your android smartphone.

3. ADB Tools. Download from here and install it on your pc.

4. Enable USB debugging on your smartphone.



5. Now Connect usb cable to pc. A dialog box will open on your smartphone saying allow USB debuuging? Select always allow from this computer and tap on OK.



6. Now its time to check whether your phone is recognized or not. Navigate to the directory where you have installed adb tools (at step 3). and Press SHIFT + Right Mouse Click in that directory into an empty space and select Open Command Prompt here.



7. To test whether ADB is working properly, connect your Android device to your computer using a USB cable and run the following command:
  1. adb devices
Copy the Code



8. After seeing the device in the list. Now you have successfully configured adb tool. Now you can use the power of ADB on Android.

Now it's time to run the adb logcat command in command prompt:-
Run the following command:-
  1. adb logcat
Copy the Code

It will start displaying the all logs of your android phone on console. Now here you will get confused and a lot of logs will be displaying continuosly which can't be even read by us. Because its too fast reading and writing the logs. So we need to filter logs.
Before filtering the logs, we need to understand the different types of logs in android. Here are those:-

V    Verbose (show all possibly useless logs, default level)
D    Debug (show all reasonable debug logs)
I    Info (show expected logs for regular usage)
W    Warn (show possible issues that are not yet errors)
E    Error (show issues that have caused errors)
F    Fatal (show issues that are fatal to runtime and will often result in rebooting)

Every Android log has priority attached with it.

The priority is one of the following character values, ordered from lowest to highest priority:
  • V — Verbose (lowest priority)
  • D — Debug
  • I — Info
  • W — Warning
  • E — Error
  • F — Fatal
  • S — Silent (highest priority, on which nothing is ever printed)


Here's an example of a filter expression that suppresses all log messages except those with the tag "ActivityManager", at priority "Info" or above, and all log messages with tag "MyApp", with priority "Debug" or above:

  1. adb logcat ActivityManager:I MyApp:D *:S
Copy the Code

The following filter expression displays all log messages with priority level "warning" and higher, on all tags:

  1. adb logcat *:W
Copy the Code

You can also filter by content. Let's say you want to collect all lines of the log that mention the term "Google". You can do this:
  1. $ adb logcat | grep Google
Copy the Code

You can also save the logcat to a text file like this:
  1. adb logcat -d > Name_of_Log_File.txt
Copy the Code
Now you can send this log file to the developer for finding the bug in the application.

Other Useful ADB Commands
In addition to the variety of tricks that require ADB, ADB offers some useful commands:
adb install C:\package.apk – Installs the package located at C:\package.apk on your computer on your device.
adb uninstall package.name – Uninstalls the package with package.name from your device. For example, you’d use the name com.rovio.angrybirds to uninstall the Angry Birds app.
adb push C:\file /sdcard/file – Pushes a file from your computer to your device. For example, the command here pushes the file located at C:\file on your computer to /sdcard/file on your device
adb pull /sdcard/file C:\file – Pulls a file from your device to your computer – works like adb push, but in reverse.
adb logcat – View your Android device’s log. Can be useful for debugging apps.
adb shell – Gives you an interactive Linux command-line shell on your device.
adb shell command – Runs the specified shell command on your device.
This is all about ADB Logcat. Hope You like it, hope you get some knowledge from it.