Guo Lin, The First Line of Code: Android
More content, welcome to my blog Xingdao
For Eclipse, you can add LogCat when you first run the project, or add it manually. The process of adding it manually is: “Click on Window Show View Other in the Eclipse navigation bar, and a Show View dialog box pops up. When you expand the Android directory in the Show View dialog box, you will see a subentry of LogCat, select LogCat, and click OK.”
For AS, just click on Android Monitor in the lower left corner to open the logcat window, as shown in the following figure:
We can also see that in the Android Monitor interface, clicking on different tabs can monitor the memory, CPU, GPU and network usage of the running simulator in real time.
Using Log Tool
We can notice that on the left side of the search box in the AS logcat interface, there is a drop-down box to select Log level. Android log files have five levels, from low to high, verbose, debug, info, warn, error. In Android’s log tool class Log (android. util. Log), there are five ways to print logs for each of these five levels:
Log. V (): Print the most trivial and meaningless log information. Corresponding level verbose.
Log. D (): Used to print some debug information, corresponding level debug.
Log. I (): Used to print some of the more important data, corresponding level info.
Log. w (): Used to print some warning information, corresponding level warn.
Log. e (): Used to print error information in programs, corresponding to the level of error.
Now let’s try using logging tools in the HelloWorld project. Open MainActivity and add a statement to print the log to the last line of the onCreate () method.
Log.d("MainActivity", "onCreate: execute");In Log. D method, two parameters are introduced. The first parameter is tag. Generally, just pass in the current class name. It is mainly used to filter printing information. The second parameter is msg, which is the specific content you want to print.
Now let’s run the HelloWorld project again. As shown in the figure below, we have received a lot of information. We can see the message we want to print in this information, but we need to look for it.
How could that happen? Let’s click on a drop-down box at the far right of the logcat interface. At this point we see a show only selected application. By choosing it, we can see that the information printed below is instantly refreshing. What is it? This is the filter provided by LogCat. Show only selected application is created automatically when we run the HelloWorld project. We see no filters, but no filters. Look at the next one, Edit Filter Configuration. Select it and we can add our own filters. The following is the add page:
Add the filter shown in the figure and select Log Level as Debug. Click OK and select. You can see the page shown below.
A filter named MainActivity filters only the specified Tag (MainActivity).
PS: For Android, using log is much better than using System. out. println () to print information, because System. out. println () has many shortcomings, such as uncontrollable log printing, indeterminate printing time, unable to add filters, and no level distinction between logs.