- Android LogCat And Logging Best Practice
- 1. Android Log Methods.
- 2. Android Log Methods Example.
- Debugging and Logging Android App
- Introduction
- Requirements
- Major problem in android app development
- Following most debugging tools and libraries for logging android app
- 1. Bugfender
- 2. Fabric Crashlytics (Firebase)
- 3. Stetho
- 4. Timber’s Requirement:
- 5. Android Debug Database
- Crash Logs on Mobile Devices
- Contents
- Motivation
- Android Logging
- iOS Logging & Crash Dumps
- Related articles and links
- Motivation
- Android Logging
- Bug report feature
- How to create Android bug reports via the developer options
- Create logs with the Android Debug Bridge (ADB)
- How to create Android logs via ADB
- Preparations
- Creating logs
- Using a batch file to render your work easier
- Troubleshooting
- iOS Logging & Crash Dumps
- Crash log files
- First option: Using your iPhone or iPad
- Second Option: Using iTunes
- Third Option: Use iTools or another software
- Creating console logs
Android LogCat And Logging Best Practice
android.util.Log is the log class that provides the log function. It provides the below methods to log data into the LogCat console.
1. Android Log Methods.
- Log.v(): Print verbose level log data. The verbose level is the lowest log level, if you print so much this kind of log data, it is not meaningful.
- Log.d(): Print debug level log data. Debug level is one step higher than verbose level. Debug log data is usually useful in android application development and testing.
- Log.i(): Print info level log data. Info level is one step higher than debug level. Info log data is used to collect user actions and behaviors.
- Log.w(): Print warn level log data. Warn level is one step higher than info level. When you see this kind of log data, it means your code exists potential risks, you need to check your code carefully.
- Log.e(): Print error level log data. Error level is the highest level. It is always used in java code catch block to log exception or error information. This kind of log data can help you to find out the root cause of app crashes.
2. Android Log Methods Example.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/ciBUMesUfVo
- This example is very simple. When you click the button, it will print above 5 kinds of log data in the LogCat console.
- When you input the search keyword LogActivity in the LogCat panel, the app log data will be filtered out. For each line of log data, there are the log time, class name, and log message.
- You can also filter out the log data by it’s type, verbose, info, debug, warn, or error. This can make log data search more easily and accurately, you can see the below demo video.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/zyUvmTF_Mzw
- Click the Settings icon in the LogCat panel, you can configure which column to be displayed for each line of the log.
- The columns include Show date and time, Show process and thread IDs(PID-TID), Show package name, Show tag, you can see it in the below picture.
Источник
Debugging and Logging Android App
Introduction
In this post, I will tell you all android logging library and debugging tool. Logging android app helps you to debug a bug and exception in local or remote. It can be production and development environment. Few things you can logging android studio
Requirements
In development mode, you are always faced errors, exceptions, and crashes. It can be tracked by android logging system or Logcat. When it’s come in production app, it not easy to debug because in the users’ device can be so many reasons for any mishappening, such as low network connectivity, device configuration, memory, CPU uses etc. I will tell you all android logging library and debugging tool that help you to find out real problem local and remote as well
Crashes and exceptions can be logged local and remote server as well. Debugging & Logging android app is compulsory to build a pro android application.
Major problem in android app development
- You can not check & view shared preference variables – View the shared preference and local database is not easy in android studio.
- For logging (standard logging) always need to give tags. Basically, In a big team follow the same logging standard by all developers is not easy. I will discuss some libraries that provide a centralized logging system in all app.
- Remotely view logs for all devices – We can debug and log locally, mean those are connected in Android Studio via USB debugging. Android logging system does not provide remote logging solution.
- Database access is very difficult – Check the database data little bit tricky in android app development
- Inspect elements – Inspect all element and network call is not easy and trackable
Following most debugging tools and libraries for logging android app
- Bugfender
- Android Debug Database
- Fabric Crashlytics (Firebase) & Answer
- Stetho
- Timber
1. Bugfender
The remote logging system for iOS and Android application. Bugfender is a best for remote logging android app. You can log each event with a custom parameter in an orgenized way. You can easily debug what went wrong of user device using logging android
Why Bugfender? At the time of development
- Logs from many devices required.
- Not possible to attach all the devices with the development machine.
- Need to check some special values on some events fire.
- The log will be saved on cloud and log file can be downloaded as well.
Features
- Logged all remote devices logs.
- You can setup cloud-based logging.
- Bugfender provides free version provides last 24 hours logs.
- Shows different logs for different devices.
- One can enable/disable devices as well as logging on those devices.
- Have different methods to print UI logs, all events logs(complete Android system logs).
- The developer can log only particular event logs.
2. Fabric Crashlytics (Firebase)
For all types crash ‘Fabric’s Crashlytics’ provide a solution for that. Fabric’s SDK logged all crashes and exceptions remotely with user device configuration
Benefits:
- Able to log errors and exception an complete stack trace for remote devices
- Gives log for debug as well as for production environment.
- It provides all details of device models.
- All the crashes and exceptions will be notified via email, every time they occur.
- Crashes reported automatically.
- To log caught exceptions crashlytics.logException(exception);
- We provide some key-value pair for more reference of exception.
Limitations
- Sometimes didn’t get complete context of a class where an error occurs.
- Android logs for crashes are sometimes difficult to understand.
3. Stetho
Stetho is mainly a debugging and logging library for Android apps. It developed by Facebook. It uses Chrome’s Developer tool to display debugging data of apps. You can debug all things in local using logging android app.
Features provided:
- View Hierarchy
- Database inspection
- Shared Preferences
- JavaScript Console
- Dumpapp
2.1 View Hierarchy: Able to view all the View elements and detailed properties (styles). The user will have a complete hierarchy view of layout including layouts which were integrated at runtime.
2.2 DataBase Inspection: User views complete database tables and also able to do the query for some specific data and also alter the data directly from here.
2.3 Shared Preferences: User can view all the shared preferences variables and also changes these values.
2.4 Network Inspection: User can view all the network request and JSON response, image preview of application.
2.5 JavaScript Console: User can interact with the application and even Android SDK by executing javascript code in the console.
2.6 Dumpapp: Provided command line interface for the Android application.
Integration: Follow the below link
User runs the code, He will see request and response in the ‘Network’ tab of Developers tool.
Inspection Process- You have done with all integration steps than move to chrome browser
- Open chrome browser in the developer system.
- Navigate to ‘chrome://inspect’.
- Here user will find all the connected devices with the Application name.
- Click ‘inspect’ to start the inspection.
4. Timber’s Requirement:
Timber is a logging library that manage all log in organised way logging android studio.
- Do not require to specify ‘TAG’ for class context like ‘Android log system. Its timber’s inbuilt feature.
- Can control which kind of logs want to show in crashlytics. eg. errors and exceptions. It will not show ‘info’ and ‘warnings’ log in crashlytics.
- We can print line-number as well with the logs.
- Provide all logging methods, provided by ‘Android logging system’. Like, debug, info, error, wtf etc
Integration:
- Follow the timber integration instruction
- Add the following in-app level Gradle file
- In Application, class add below code
Now timber provides all kind of logging eg. debug, info, verbose etc in ‘Developer tool’s console’. Do not need to add ‘TAG’, just add messages where ever it is required.
Limitations
- Do not work for remote devices.
- Logging can’t be saved.
5. Android Debug Database
This android logging library allows you to view databases & shared preferences in your development machine browser.
Features
- View database.
- Run sql query to modify or delete data from DB.
- View/edit/delete shared preferences of application.
- Directly edit the database values.
- Search and sort data.
- Download database.
Limitations
- Development machine & device should be in the same network.
Integration: Follow the link
Источник
Crash Logs on Mobile Devices
Contents
Motivation
Android Logging
Bug report feature
How to create Android bug reports via the developer options
Create logs with the Android Debug Bridge (ADB)
How to create Android logs via ADB
Using a batch file to render your work easier
iOS Logging & Crash Dumps
Crash log files
First option: Using your iPhone or iPad
Second Option: Using es
Third Option: Use iTools or another software
Creating console logs
Related articles and links
Motivation
In case of crashes in a mobile app, devices logs are mandatory as a key component of your bug report. They can also be useful for app freezes. Only logged information can provide detailed insight into the circumstances of the issue.
In this article, we will demonstrate common solutions to retrieving log information for both iOS and Android devices. Applying these techniques will allow you to record and attach meaningful log files to your reports.
Android Logging
Depending on the situation and the available devices, there are two main approaches to retrieving Android device logs:
Bug report feature
With the Android bug report feature directly in the developer settings on your device, you can create bug reports at any time, which will include all log activity within a certain time frame depending on the size of your log buffer and the logg activity on your device. This time frame can reach from a couple of minutes up to several hours or even days.
How to create Android bug reports via the developer options
Enable the developer options on your phone:
Open the settings and navigate to System > About your phone.
Tap the Build Number 7 times.
Navigate back to Settings > System.
Find the Developer options.
Tap Take bug report and, if asked, pick the Interactive report.
Monitor the report creation progress in your notifications.
Once ready, share the report by sending yourself an email or by uploading it to a target of your choice like Google Drive or Dropbox.
Tailor the log output to the crash:
Open the report text file on your computer (unzip it first if required).
Find the SYSTEM LOG section of the report.
If you know the time of the crash, use the timestamp to locate it.
If you do not know the time, you can search for keywords like exception, crash or has died, which are related to the app under test.
Extract the relevant part including some time before and after the bug event.
Please have a look at the following example of a crash event, which includes the stack trace of the error event and allows a developer to locate the bug:
Create logs with the Android Debug Bridge (ADB)
By utilizing the Android Debug Bridge, you are able to create log files that cover a certain time frame. Those files are smaller and focus on a particular issue that should ideally be easily reproducible. These logs do not require you to manually look for crash events and are the preferred method to create meaningful logs for reproducible issues.
How to create Android logs via ADB
Numerous tools are available for creating an ADB connection to your phone. A very small and versatile one is Minimal ADB and Fastboot.
Preparations
The following steps need to be performed only once:
Download Minimal ADB and Fastboot here or use any other source.
Install the software or open the program folder if you chose the portable version.
Prepare your device for using USB debugging.
Connect your phone to your computer.
On your phone, head to Settings > System > Developer options.
Look for an option similar to USB Debugging, enable it, and confirm the warning message that may appear.
Upon the first connection to your computer, confirm the security warning and tick Always allow from this computer to avoid future warnings.
Creating logs
On your computer, open Minimal ADB and Fastboot either.
… via the start menu (if you installed it) or…
… head to the folder of the portable version, hold the SHIFT key, right-click an empty spot of the folder, and select Open command prompt here – respectively Open PowerShell here on Windows 10 machines.
Connect your phone to your computer.
Within Minimal ADB, enter the following command to check the successful connection: adb devices
Your device should be listed among the connected devices.
If you want to create a log for a reproducible issue, first clear the log history to remove unnecessary content by entering: adb logcat -c
To start the actual logging, proceed as follows:
Choose or create a folder on your computer, hold SHIFT and right-click the folder
In the context menu, select Copy as path.
Within Minimal ADB, enter: adb logcat -v time > and paste your path by right-clicking the log window whereafter it may look like this: adb logcat -v time > «D:\Logs»
Now, add a file name to the path that will then look like this (please be sure to use the TXT file extension): adb logcat -v time > «D:\Logs\log.txt»
Confirm by pressing Return.
Reproduce the bug or crash for which you want to create the log. Although nothing happens in the log window, Minimal ADB is recording the logg activity in the background.
Once you are done, press CTRL + C to stop the logging.
You will now find your log file in the specified folder.
Using a batch file to render your work easier
To avoid having to enter all commands manually each time, you can create a batch file to automate these tasks. For that purpose, proceed as follows:
Download the portable version of Minimal ADB and Fastboot and navigate to the extracted folder.
Create a new text file in this folder and open it in an editor like Windows Editor.
Add the following content: @ECHO OFF; echo Clearing the log history…; adb logcat -c; echo Starting live logging. Press CTRL + C to stop.; adb logcat > device_log.txt
Save the file as log.bat and make sure that .bat is the actual file extension.
Whenever you execute this file, a new log file will be created in the current folder. The old ones will be overwritten.
Troubleshooting
If Minimal ADB does not recognize your phone, you can use an alternative driver like the Universal ADB Driver, which can be found here. Disconnect your phone, install the driver, and connect your phone again. Afterwards, try to create a log again.
Also, make sure that your phone is not set to Charge only if it is connected to your computer. Select File transfer or Media transfer instead.
iOS Logging & Crash Dumps
Obtaining log information on iOS devices does not require much effort and usually no access to a computer. Different types of log information can be obtained and we want to cover several approaches of retrieving them.
Crash log files
The most common type of log data is the crash report file that is automatically created in the background once a crash occurs on your iOS device. Gathering this file and attaching it to your bug report will be sufficient for the majority of crashes. Several approaches are available to obtain these files and we would like to cover three of them.
First option: Using your iPhone or iPad
You can access a history of crash reports directly on your phone and without involving a computer. For this purpose, proceed as follows:
On your phone or tablet, head to the Settings and tap Privacy.
Proceed to Analytics and Analytics Data.
Review the list of reports. The list is sorted alphabetically. Look for the name of the affected app and an appropriate timestamp.
Once found, tap on the report.
Share its content to an app of your choice and attach it to your bug report.
Second Option: Using iTunes
If you have an older macOS version installed or you are on a Windows computer, you can use iTunes to access all crash reports on your phone – just a synchronisation is required:
Connect your iPhone or iPad to your computer.
If not already present, install iTunes.
Open iTunes and connect your phone or tablet via USB.
Wait for the automatic synchronisation or trigger it manually.
Find the reports folder on your computer:
Windows: C:\Users\ \AppData\Roaming\Apple Computer\Logs\CrashReporter\MobileDevice\
Locate the appropriate .crash or .ips file based on the app name and timestamp.
Attach it to your bug report.
Third Option: Use iTools or another software
Alternatively, you can use software like iTools to access the crash reports on your phone. To do so, proceed as follows:
Download, install and open iTools.
Connect your phone via USB and confirm the driver installation if required.
Click Toolbox in the top area of the app.
Click Crash Reports.
Select an item based on the app name and timestamp.
Click Export to save the file and attach it to your bug report.
Creating console logs
Sometimes, you might not be able to find and provide a crash log file as the app might not have actually crashed but showed a different critical behavior. Alternatively, you may be asked to provide further information regarding the circumstances of an issue where console logs can be used to provide further insight.
There are several approaches to gathering console logs but we want to cover an easy approach using the software iTools, which is available as a free trial version:
Download and install iTools.
Connect your phone via USB and confirm the driver installation if required
Click Toolbox in the top area of the app.
Click Console Log.
Reproduce your issue while capturing the log.
Click Clear to reset the log history before beginning the reproduction if necessary.
Reproduce the crash or faulty behavior on your phone.
Click Save full Log.
Provide a file name and confirm it.
Attach the console log file to your bug report.
Источник