不同的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过滤,它将很高兴知道我在适当的时候使用适当的方法。
当前回答
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.
一般来说,您是对的,它基本上是任意的,由您来定义什么是调试日志、什么是信息日志、什么是错误日志等等。
其他回答
源代码提供了一些基本的指导:
从最少到最多的顺序是ERROR, WARN, INFO, DEBUG, VERBOSE。除非在开发期间,否则决不应该将Verbose编译到应用程序中。调试日志在运行时被编译,但在运行时被剥离。错误、警告和信息日志总是被保存。
要了解更多细节,柯蒂斯的回答非常中肯。我只想补充一句:不要在INFO或以上(警告/错误)记录任何个人身份或私人信息。否则,错误报告或任何包含日志记录的内容都可能受到污染。
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
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.
一般来说,您是对的,它基本上是任意的,由您来定义什么是调试日志、什么是信息日志、什么是错误日志等等。
让我们倒着来看看:
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 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.