Using the Android log tool Logcat

Source: Internet
Author: User

Android

The Log tool class in is log (ANDROID.UTIL.LOG), which provides several ways for us to print the log.

1. LOG.V ()

This method is used to print the most trivial, least meaningful log information. The corresponding level verbose, is

The lowest level of Android logs.

2. LOG.D ()

This method is used to print some debugging information that should be helpful for debugging your program and analyzing the problem. The corresponding level of debug, higher than the verbose level.

3. LOG.I ()

This method is used to print some of the more important data that you would like to see that can help you analyze user behavior. corresponding level info, higher than Debug.

4. LOG.W ()

This method is used to print some warning messages, suggesting that the program may have potential risks in this place, and it is best to fix these warnings. Corresponds to level warn, one level higher than info.

5. LOG.E ()

This method is used to print error messages in a program, such as a program entering a catch statement. When the error message is printed, it usually means that your program has a serious problem and must be repaired as soon as possible. The corresponding level of error, higher than the warn level. Actually very simple, altogether has five methods, certainly each method also may have the different overloading, but that to you certainly not

What a difficult place to understand. Let's try the log tool in the HelloWorld project now. It's not good to use. Open helloworldactivity and add a line of print log statements in the OnCreate () method, as follows:

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.hello_world_layout);

LOG.D ("Helloworldactivity", "onCreate execute");

}

Log.d method passed two parameters, the first parameter is tag, generally passed in the current class name is good, mainly used to filter the printing information. The second parameter is MSG, which is the specific content you want to print.

You can now rerun the HelloWorld project, still right-click the HelloWorld project →run as

→android application. After the program has finished running, you can see the print information in LogCat 1.20 shown.

Figure 1.20

You can see not only the contents of the print log and the Tag name, but also the package name of the program, the time of printing, and the process number of the application. If you don't have any information printed in your LogCat, it's possible that your current device is losing focus. Then you just have to go to the DDMS view, click on your current device in the Devices window, and the print message will come out.

Also do not know whether you notice, your first line of code has been unknowingly written out, I have finally done.

1.4.3 Why use Log instead of System.out

I'm sure a lot of Java novices are very fond of using the System.out.println () method to print logs, and don't know if you'd like to do the same. However, in real project development, it is extremely not recommended to use the SYSTEM.OUT.PRINTLN () Method! If you use this method frequently in your company's projects, you are likely to be scolded.

Why would the System.out.println () method be so despised by everyone? After careful analysis, I found that this method is useless except for the convenience of use. Where is the convenience? In Eclipse you only need to enter SYSO, and then press the code hint key, this method will automatically come out, I believe this is also a lot of Java novice love for it reasons. And where is that shortcoming? This is too much, such as the log printing is not controllable, printing time is not determined, cannot add filters, log no level discrimination ...

Listen to me, you probably don't want to use the System.out.println () method, so Log put all the shortcomings mentioned above all done? Not all, but I think the Log has done quite well. I'm going to show you the strength of the Log and LogCat mates right now.

First of all, in LogCat you can easily add filters, and you can see all of our current filters in Figure 1.21.

Figure 1.21

There are currently only two filters, and all messages filters are equivalent to no filters, which will show all the logs. Com.test.helloworld filter is created automatically when we run the HelloWorld project, click this filter to see only the logs printed in the HelloWorld program. Can you customize the filter? Currently, let's add a filter to try it out.

Click the plus sign in Figure 1.21 to bring up a filter configuration screen. We name the filter data and let it filter the Tag named data, as shown in 1.22.

Figure 1.22

Click OK, you will find that you have more than one data filter, when you click on this filter, you will find just in the OnCreate () method printed in the log is not, this is because the data filter will only display the Tag name is called data log. You can try to change the print log statement to LOG.D ("Data", "onCreate execute") in the OnCreate () method, and then run the program again, and you'll see this line of logs under the data filter.

I don't know if you realize the benefits of using filters, maybe not now. But when your program prints hundreds of thousands of lines of logs, you'll need a filter urgently.

After reading the filter, take a look at the log level control in LogCat. There are 5 main levels in LogCat, which correspond to the 5 methods I described in the previous section, as shown in 1.23.

Figure 1.23

The current level we select is verbose, which is the lowest level. This means that no matter which method we use to print the log, this log will definitely show up. And if we check the level to debug, then only the logs we print with Debug and above are displayed, and so on. You can do the experiment, and if you select the level in LogCat as info, warn, or error, the statements we print in the OnCreate () method are not displayed because we are using the LOG.D () method when we print the log.

The advantage of Log level control is that you can quickly find the logs you care about. Believe that if you look for a crash message from thousands of logs, you're going to freak out. Now you just have to select the log level as error, and irrelevant trivial information will no longer interfere with your vision.

Using the Android log tool Logcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.