在Android的为什么不应该使用的System.out.println() [英] Why shouldn't I use System.out.println() in android
问题描述
在此 code风格他们说,我们不应该用系统.out.println(),但我不理解他们的想法。可以对任何人解释一下吗?我应该用它来跟踪我的应用程序的日志?
的System.out.println()(或printf()的原生code)不应该被使用。的System.out和System.err重定向到/ dev / null的,让您的打印报表不会有任何明显的影响。但是,所有的字符串建筑,仍然发生了这些要求得到执行。
这是我的问题太多。您应该使用登录类的方法。
作为Android开发者,这些都是记录工具的常用方法
-
Log.d()
- 调试 -
Log.e()
- 错误 -
Log.i()
- 信息 -
Log.w()
- 警告 -
Log.v()
- 详细
还有一个方法 Log.wtf()
。就个人而言,我不知道用法:)
方法有两个参数
-
字符串,字符串
- 这是它是一个标签和消息基本参数 -
字符串,字符串,可抛出
- 拥有相似的第一位。但是,如果你想记录一个异常
或的Throwable
S在logcat中,除调用的printStackTrace()
用这个。
修改:直接去回答你的问题。 的println()
的的System.out
和 System.err的
还是会显示在logcat中,但受到限制。
- 您无法登录
VERBOSE
,错误
或DEBUG
使用的System.out
或System.err的
。 -
您不能定义自己的
标记
,就会显示System.err的
或的System.out
您的文字。作为等价-
的System.out.println(你好!)
等同于Log.i(System.out的,你好! )
-
通信System.err.println(你好!)
等同于Log.w(System.err的,你好! )
-
In this code style they said that we shouldn't use System.out.println() but I don't understand their's idea. can Anyone explain about that? What should I use to trace the log of my app?
System.out.println() (or printf() for native code) should never be used. System.out and System.err get redirected to /dev/null, so your print statements will have no visible effects. However, all the string building that happens for these calls still gets executed.
It was my problem too. You should use class methods of Log.
As android developer, these are the commonly used method of Logging utility
Log.d()
- DebugLog.e()
- ErrorLog.i()
- InformationLog.w()
- WarningLog.v()
- Verbose
There's also a method Log.wtf()
. Personally, I don't know that usage :)
Methods above have both parameters
String , String
- This is a basic parameter which is a TAG and Message.String, String, Throwable
- Has similarity on first one. However, If you want to log anException
or anyThrowable
s in logcat, other than callingprintStackTrace()
use this.
EDIT: Going straight to answer your question. println()
of System.out
and System.err
will still displayed in logcat, but with limitations.
- You can't log
VERBOSE
,ERROR
, orDEBUG
usingSystem.out
orSystem.err
. You can't define your own
TAG
, it will displaySystem.err
orSystem.out
with your text. As equivalenceSystem.out.println("Hello!")
is equivalent toLog.i("System.out","Hello!")
System.err.println("Hello!")
is equivalent toLog.w("System.err","Hello!")
这篇关于在Android的为什么不应该使用的System.out.println()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!