| Start | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Filtering the logcat output It is helpful to filter the logcat output because there are many messages which are not of interest. To filter the output, open the "Android Monitor" and click on the drop down on the top-right and select Edit Filter Configuration Now you can add custom filters to show messages which are of interest, as well as filter out well-known log lines which can safely be ignored. To ignore a part of the output you may define a Regular Expression (opens new window). Here is an example of excluding matching tags: This can be entered by following this example: The above is a regular expression which excludes inputs. If you wanted to add another tag to the blacklist, add it after a pipe For more documentation and examples visit Logging and using Logcat (opens new window) |
||||||||||||||||||||
| Logging Any quality Android application will keep track of what it's doing through application logs. These logs allow easy debugging help for the developer to diagnose what's going on with the application. Full Android Documentation can be found here (opens new window), but a summary follows: Basic LoggingThe `Log` class is the main source of writing developer logs, by specifying a `tag` and a `message`. The tag is what you can use to filter log messages by to identify which lines come from your particular Activity. Simply callAnd the Android system will write a message to the logcat: TIP: Any tag can be used, but it is common to use the class name as a tag: Log LevelsThe Android logger has 6 different levels, each of which serve a certain purpose:
Motivation For LoggingThe motivation for logging is to easily find errors, warnings, and other information by glancing at the chain of events from the application. For instance, imagine an application that reads lines from a text file, but incorrectly assumes that the file will never be empty. The log trace (of an app that doesn't log) would look something like this:Followed by a bunch of stack traces that would eventually lead to the offending line, where stepping through with a debugger would eventually lead to the problem However, the log trace of an application with logging enabled could look something like this: A quick glance at the logs and it is obvious that the file was empty. Things To Considering When Logging:Although logging is a powerful tool that allows Android developers to gain a greater insight into the inner working of their application, logging does have some drawbacks.Log Readability:It is common for Android Applications to have several logs running synchronously. As such, it is very important that each log is easily readable and only contains relevant, necessary information.Performance:Logging does require a small amount of system resources. In general, this does not warrant concern, however, if overused, logging may have a negative impact on application performance.Security:Recently, several Android Applications have been added to the Google Play marketplace that allow the user to view logs of all running applications. This unintended display of data may allow users to view confidential information. As a rule of thumb, always remove logs that contain on non-public data **before** publishing your application to the marketplace.Conclusion:Logging is an essential part of an Android application, because of the power it gives to developers. The ability to create a useful log trace is one of the most challenging aspects of software development, but Android's Log class helps to make it much easier.For more documentation and examples visit Logging and using Logcat (opens new window) |
||||||||||||||||||||
| Using the Logcat Logcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log class. The Logcat output can be displayed within Android Studio's Android Monitor or with adb command line. In Android Studio Show by clicking the "Android Monitor" icon:
via command line: Simple usage: With timestamps: Filter on specific text: There are many options and filters available to command line logcat, documented here (opens new window). |
||||||||||||||||||||
| Log with link to source directly from Logcat This is a nice trick to add a link to code, so it will be easy to jump to the code that issued the log. With the following code, this call:
This is the code (inside a class called MyLogger): This is a basic example, it can be easily extended to issue a link to the caller (hint: the stack will be [4] instead of [3]), and you can also add other relevant information. |
||||||||||||||||||||
| Generating Logging code
Examples:
Full list of templates can be found in If you find This is a very useful library which helps you to avoid writing the logging line manually. The syntax is absolutely simple: .log - Logging. If there is constant variable "TAG", it use "TAG" . Else it use class name. |
||||||||||||||||||||
| Android Studio usage Hide/show printed information:
[ Control verbosity of the logging:
[ Disable/enable opening log window when starting run/debug application
[ |
||||||||||||||||||||
| Clear logs In order to clear (flush) the entire log: Syntax
Parameters
RemarksDefinitionLogcat is a command-line tool that dumps a log of system messages, including stack traces when the device throws an error and messages that you have written from your app with the Log (opens new window) class. When to useIf you are considering using Java's System.out methods for printing to the console instead of using one of Android's Log methods, then you should know that they basically work the same. However, it is better to avoid using Java's methods because the extra information and formatting provided by Android's Log methods are more beneficial. Also, the System.out print methods are redirected (opens new window) to the Useful links
|
Read Article
https://develop.horje.com/learn/1434/reference