不同的LogCat方法有:

Log.v(); // Verbose
Log.d(); // Debug
Log.i(); // Info
Log.w(); // Warning
Log.e(); // Error

哪种情况适合使用每种类型的日志记录?我知道也许这只是一点语义,也许它真的不重要,但在Android Studio和Eclipse中的LogCat过滤,它将很高兴知道我在适当的时候使用适当的方法。


让我们倒着来看看:

Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch statement. You know that an error has occurred and therefore you're logging an error. Log.w: Use this when you suspect something shady is going on. You may not be completely in full on error mode, but maybe you recovered from some unexpected behavior. Basically, use this to log stuff you didn't expect to happen but isn't necessarily an error. Kind of like a "hey, this happened, and it's weird, we should look into it." Log.i: Use this to post useful information to the log. For example: that you have successfully connected to a server. Basically use it to report successes. Log.d: Use this for debugging purposes. If you want to print out a bunch of messages so you can log the exact flow of your program, use this. If you want to keep a log of variable values, use this. Log.v: Use this when you want to go absolutely nuts with your logging. If for some reason you've decided to log every little thing in a particular part of your app, use the Log.v tag.

作为奖励…

日志。wtf:当事情完全,可怕的,该死的错误时使用这个。你知道那些catch块,你在那里捕捉你永远不应该得到的错误……是的,如果你想记录他们使用log .wtf


I think the point of those different types of logging is if you want your app to basically self filter its own logs. So Verbose could be to log absolutely everything of importance in your app, then the debug level would log a subset of the verbose logs, and then Info level will log a subset of the debug logs. When you get down to the Error logs, then you just want to log any sort of errors that may have occured. There is also a debug level called Fatal for when something really hits the fan in your app.

一般来说,您是对的,它基本上是任意的,由您来定义什么是调试日志、什么是信息日志、什么是错误日志等等。


The different methods are indications of priority. As you've listed them, they're going from least to most important. I think how you specifically map them to debug logs in your code depends on the component or app you're working on, as well as how Android treats them on different build flavors (eng, userdebug, and user). I have done a fair amount of work in the native daemons in Android, and this is how I do it. It may not apply directly to your app, but there may be some common ground. If my explanation sounds vague, it's because some of this is more of an art than a science. My basic rule is to be as efficient as possible, ensure you can reasonably debug your component without killing the performance of the system, and always check for errors and log them.

V -在不同的时间间隔或在我的组件处理的任何事件发生时的状态打印输出。还可能是我的组件接收或发送的消息/事件有效负载的非常详细的打印输出。

D -组件中发生的小事件的详细信息,以及组件接收或发送的消息/事件的有效负载。

I -我的组件接收或发送的任何消息/事件的头部,以及对我的组件的操作至关重要的有效负载的任何重要部分。

W -发生的任何不寻常或可疑的事情,但不一定是错误。

E -错误,指的是当事情正常工作时不应该发生的事情。

The biggest mistake I see people make is that they overuse things like V, D, and I, but never use W or E. If an error is, by definition, not supposed to happen, or should only happen very rarely, then it's extremely cheap for you to log a message when it occurs. On the other hand, if every time somebody presses a key you do a Log.i(), you're abusing the shared logging resource. Of course, use common sense and be careful with error logs for things outside of your control (like network errors), or those contained in tight loops.

也许不好

Log.i("I am here");

Good

Log.e("I shouldn't be here");

With all this in mind, the closer your code gets to "production ready", the more you can restrict the base logging level for your code (you need V in alpha, D in beta, I in production, or possibly even W in production). You should run through some simple use cases and view the logs to ensure that you can still mostly understand what's happening as you apply more restrictive filtering. If you run with the filter below, you should still be able to tell what your app is doing, but maybe not get all the details.

logcat -v threadtime MyApp:I *:S

源代码提供了一些基本的指导:

从最少到最多的顺序是ERROR, WARN, INFO, DEBUG, VERBOSE。除非在开发期间,否则决不应该将Verbose编译到应用程序中。调试日志在运行时被编译,但在运行时被剥离。错误、警告和信息日志总是被保存。

要了解更多细节,柯蒂斯的回答非常中肯。我只想补充一句:不要在INFO或以上(警告/错误)记录任何个人身份或私人信息。否则,错误报告或任何包含日志记录的内容都可能受到污染。


Android Studio网站最近(我认为)提供了一些建议,从不同的日志级别中期待什么样的消息可能是有用的,以及Kurtis的回答:

Verbose - Show all log messages (the default). Debug - Show debug log messages that are useful during development only, as well as the message levels lower in this list. Info - Show expected log messages for regular usage, as well as the message levels lower in this list. Warn - Show possible issues that are not yet errors, as well as the message levels lower in this list. Error - Show issues that have caused errors, as well as the message level lower in this list. Assert - Show issues that the developer expects should never happen.


你可以使用LOG,比如:

Log.e(String, String) (error)
Log.w(String, String) (warning)
Log.i(String, String) (information)
Log.d(String, String) (debug)
Log.v(String, String) (verbose)

示例代码:

private static final String TAG = "MyActivity";
...
Log.i(TAG, "MyClass.getView() — get item number " + position);

尽管这个问题已经被回答了,但我觉得在已经回答的答案中还缺少一些例子。

因此,我将把我在博客文章“Android日志级别”中所写的内容带到这里。

详细的

Is the lowest level of logging. If you want to go nuts with logging then you go with this level. I never understood when to use Verbose and when to use Debug. The difference sounded to me very arbitrary. I finally understood it once I was pointed to the source code of Android¹ “Verbose should never be compiled into an application except during development.” Now it is clear to me, whenever you are developing and want to add deletable logs that help you during the development it is useful to have the verbose level this will help you delete all these logs before you go into production.

调试

Is for debugging purposes. This is the lowest level that should be in production. Information that is here is to help during development. Most times you’ll disable this log in production so that less information will be sent and only enable this log if you have a problem. I like to log in debug all the information that the app sends/receives from the server (take care not to log passwords!!!). This is very helpful to understand if the bug lies in the server or the app. I also make logs of entering and exiting of important functions.

Info

用于突出显示应用程序进度的信息性消息。例如,当应用程序初始化完成时。当用户在活动和片段之间移动时添加信息。记录每个API调用,但只记录URL、状态和响应时间等少量信息。

警告

当出现潜在的有害情况时。

This log is in my experience a tricky level. When do you have a potential harmful situation? In general or that it is OK or that it is an error. I personally don’t use this level much. Examples of when I use it are usually when stuff happens several times. For example, a user has a wrong password more than 3 times. This could be because he entered the password wrongly 3 times, it could also be because there is a problem with a character that isn’t being accepted in our system. Same goes with network connection problems.

错误

错误的事件。错误发生后,应用程序仍然可以继续运行。例如,当我得到一个空指针时,我不应该得到一个空指针。解析服务器的响应时出错。从服务器得到一个错误。

WTF(多么可怕的失败)

Fatal用于导致应用程序退出的严重错误事件。在Android中,致命的实际上是错误级别,不同之处在于它还添加了全堆栈。