Log output of Android development notes

I. Introduction

        Logging is the process of recording the operation and state of the application to an auxiliary interface (such as a file, or a database, whichever works). However, software developers don't take logging very seriously. Actually useful logs can be a huge help to developers (especially when someone has to debug/maintain someone else's code) when trying to understand what the code is doing. Some developers say that stack traces are something people should need, but that couldn't be further from the truth. Stack traces are great for telling you where and what went wrong, but they can't tell you how you got there in the first place. Of course, you can trace execution through breakpoints, however, stepping in blindly makes the whole process more time-consuming than it should and could be.

        All in all, whether you need to train an artificial intelligence model or write a reliable software program, I strongly recommend that you add logging during the development process to better maintain the code.

2. Why use Log log output instead of print function printing in Android development

        Many programmers like to use System.out.println() for debugging because it is extremely convenient. But in the Android development process, this is not recommended. It is difficult to find the printed information by using System.out.println(), because the output of Android is too much, if you don’t pay attention, the things just printed will be gone (it will be topped by other Log output). The more official reason is that System.out.println() has no printing level distinction, no log label, and it is not convenient to save to a file.

        If you use Log, you can use the log label and distinguish the log level very well. At the same time, add a filter to the Logcat in the Android development environment to locate the printed information well.

3. Different methods of Android log output

Log output method log level Remark
Log.v() verbose Print trivial, less meaningful log information
Log.d() debug print some debug information
Log.i() information Print some important data
Log.w() warn print some warning messages
Log.e() error Error messages in the print program

        There are five ways to print logs above, please print according to the log level, do not use it wrong (for example, print trivial information through the Log.e method).

4. References

        1、An introduction to logging for programmers

        2、The Art of Logging

        3. Guo Lin, the first line of code Android version 3

Guess you like

Origin blog.csdn.net/qq_36158230/article/details/131826086