为什么我不应该在 android 中使用 System.out.println() [英] Why shouldn't I use System.out.println() in android
问题描述
在Android开源项目的代码风格中,它指出我们应该不使用 System.out.println()
但我不明白为什么.谁能解释一下?我应该使用什么来跟踪我的应用日志?
In the Android Open Source Project's code style, it states that we shouldn't use System.out.println()
but I don't understand why. Can anyone explain? What should I use to trace my app's log?
以下是供参考的行:
System.out.println()
(或 printf()
用于本机代码)永远不应使用.System.out
和 System.err
被重定向到 /dev/null
,所以你的打印语句将没有可见的效果.但是,为这些调用发生的所有字符串构建仍然会被执行.
System.out.println()
(orprintf()
for native code) should never be used.System.out
andSystem.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.
推荐答案
你应该使用 android.util.Log
类.
You should use the android.util.Log
class.
以下是对 Log
类的作用的描述:
Here's a description of what the Log
class does:
用于发送日志输出的 API.
API for sending log output.
一般来说,你应该使用Log.v()
、Log.d()
、Log.i()
、Log.w()
和 Log.e()
方法来写入日志.然后您可以查看logcat中的日志.
Generally, you should use the Log.v()
, Log.d()
, Log.i()
, Log.w()
, and Log.e()
methods to write logs. You can then view the logs in logcat.
就详细程度而言,从最少到最多的顺序是ERROR、WARN、INFO、DEBUG、VERBOSE.除非在开发过程中,否则永远不应将 Verbose 编译到应用程序中.调试日志被编译但在运行时被剥离.错误、警告和信息日志始终保留.
The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
这些是 Log
类的可用方法:
These are the available methods of the Log
class:
Log.d()
- 发送一个DEBUG
日志消息.Log.e()
- 发送一个ERROR
日志消息.Log.i()
- 发送一个INFO
日志消息.Log.v()
- 发送一个VERBOSE
日志消息.Log.w()
- 发送一个WARN
日志消息.Log.wtf()
- 多么可怕的失败:报告一个不应该发生的异常.
上述方法(Log.w
和 Log.wtf
除外,它们有 3 种可能的参数模式)需要以下参数:
The methods above (with the exception of Log.w
and Log.wtf
which have 3 possible patterns of arguments) require the following arguments:
String tag, String msg
:
tag
:用于标识日志消息的来源.该值可能是 null
.
tag
: Used to identify the source of a log message. This value may benull
.
msg
:您想要记录的消息.该值可能是 null
.
msg
: The message you would like logged. This value may be null
.
String tag, String msg, Throwable tr
- 类似于第一个模式,但允许指定异常.如果您想将异常记录到日志输出中,则应使用此模式.
String tag, String msg, Throwable tr
- Similar to the first pattern, but allows for an exception to be specified. This pattern should be used if you want to log an exception to the log output.
(For Log.w
and Log.wtf
) String tag, Throwable tr
类似于第三种模式,但不不允许指定消息.请注意,您仍然可以传递消息,但它应该位于参数的第二种排列中.
(For Log.w
and Log.wtf
) String tag, Throwable tr
Similar to the third pattern, but does not allow for a message to be specified. Note that you can still pass a message but it should be in the second arrangement of arguments.
<小时>
EDIT:直接回答您的问题:System.out
和 System.err
的 println()
> 仍会显示在 logcat 中,但有限制.
EDIT: Going straight to answer your question: println()
of System.out
and System.err
will still be displayed in logcat but with limitations.
- 您不能使用
System.out
或记录
.VERBOSE
、ERROR
或DEBUG
System.err 您不能定义自己的标签,它会在您的文本中显示
System.err
或System.out
.例如:
- You can't log
VERBOSE
,ERROR
, orDEBUG
usingSystem.out
orSystem.err
. You can't define your own tag, it will display
System.err
orSystem.out
with your text. For instance:
System.out.println("Hello!")
等价于Log.i("System.out","Hello!")
System.err.println("Hello!")
等价于Log.w("System.err","Hello!")
System.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屋!