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