不同的LogCat方法有:
Log.v(); // Verbose
Log.d(); // Debug
Log.i(); // Info
Log.w(); // Warning
Log.e(); // Error
哪种情况适合使用每种类型的日志记录?我知道也许这只是一点语义,也许它真的不重要,但在Android Studio和Eclipse中的LogCat过滤,它将很高兴知道我在适当的时候使用适当的方法。
不同的LogCat方法有:
Log.v(); // Verbose
Log.d(); // Debug
Log.i(); // Info
Log.w(); // Warning
Log.e(); // Error
哪种情况适合使用每种类型的日志记录?我知道也许这只是一点语义,也许它真的不重要,但在Android Studio和Eclipse中的LogCat过滤,它将很高兴知道我在适当的时候使用适当的方法。
当前回答
尽管这个问题已经被回答了,但我觉得在已经回答的答案中还缺少一些例子。
因此,我将把我在博客文章“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中,致命的实际上是错误级别,不同之处在于它还添加了全堆栈。
其他回答
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.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
尽管这个问题已经被回答了,但我觉得在已经回答的答案中还缺少一些例子。
因此,我将把我在博客文章“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中,致命的实际上是错误级别,不同之处在于它还添加了全堆栈。
你可以使用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);
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.
一般来说,您是对的,它基本上是任意的,由您来定义什么是调试日志、什么是信息日志、什么是错误日志等等。