In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to output and filter LogCat according to the level of log information in Android program debugging, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Android program debugging; LogCat
No matter what kind of program development process, errors are inevitable. In general, syntax errors will be detected by the development environment, and can prompt us for the location of errors and ways to modify them in time. But logic errors are not so easy to find. Usually, the location and analysis of logic errors is a very difficult thing.
So in the process of learning Android, after learning its program structure, writing HelloWorld and some simple interface layout programs, I chose to study its debugging tools. LogCat is one of them. LogCat is a tool for obtaining system log information. The information it can capture includes information generated by Dalvik virtual machine, process message, ActivityManager information, PackagerManager information, Android runtime information and application information and so on.
After opening Eclipse, select Window-> Show View-> Other menu, and then select LogCat in Android- > LogCat, so that LogCat will appear in the lower area of Eclipse.
Among them, the five letters at the top right of the LogCat represent five different types of log information (distinguished by different colors, the higher the level, the more prominent the color):
1. [v]: detailed (Verbose) information, output color is black
two。 [d]: debugging (Debug) message, output color is blue
3. [I]: advertise (Info) message, output color is green
4. [W]: warning (Warn) message, output color is orange
5.
In LogCat, we can advertise the five-letter icon to select the type of information to be displayed, and information with a level higher than the selected type will also be displayed in LogCat, but information with a level lower than the selected type will not be displayed.
In the process of debugging the Android program, we should first introduce the android.util.Log package, and then use five functions to set the "log point" in the program, such as Log.v () for recording detailed information, Log.d () for recording debugging information, Log.i () for recording notification information, Log.w () for recording warning information, and Log.e () for recording error information. The * parameter of these functions is the log tag TAG (that is, you need a constant to mark the location where you want to test, and the name of the tag is the so-called log tag), and the second parameter is the actual information content. Whenever the program runs to the "log point" that we set, the log information of the application will be sent to LogCat, and we can judge whether there is an error in the program according to whether the information displayed in the "log point" is consistent with the information we set in the function, so that we can quickly find the wrong "accident site" according to the "log point".
In the following program, the specific use of the Log class is demonstrated:
@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); tv = (TextView) findViewById (R.id.label); et = (EditText) findViewById (R.id.entry); btn1 = (Button) findViewById (R.id.cancel); btn2 = (Button) findViewById (R.id.ok) Tv.setText (R.string.name); btn1.setText (R.string.button1); btn2.setText (R.string.button2); Log.v (TAG, "This is Verbose"); Log.d (TAG, "This is Debug"); Log.i (TAG, "This is Info"); Log.w (TAG, "This is Warn")
After the program runs, LogCat captures the log information sent by the application, and the result is shown in the following figure:
Through the results, we find that even if we choose a specific level of log information in advance, there is still a lot of log information, which needs us to read one by one, which brings us a lot of trouble. At this point, we are going to use a "filter" function provided by LogCat. The "+" sign and "-" sign in the upper right corner are adding and removing filters, respectively. We can filter the displayed log content according to the label of the log information (Tag), the process number of the generated log (Pid), or the information level (Level). In practice, we declare a string constant TAG for each class, so that we can easily distinguish the logs of different classes in Logcat.
Use the "+" sign in the upper right corner of LogCat to add a filter named LogcatFilter, and set the filter condition to "tag = LOGCAT". The following figure shows how to set the filter:
After the filter is set, the log information filtered by LogcatFilter is shown in the following figure, so that no matter what type of log information and which process it belongs to, as long as it is labeled LOGCAT, it will be displayed in the LogcatFilter.
The answer to the question about how LogCat outputs and filters according to the log information level in Android program debugging is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 246
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.