在Android的为什么不应该使用的System.out.println() [英] Why shouldn't I use System.out.println() in android

查看:910
本文介绍了在Android的为什么不应该使用的System.out.println()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此 code风格他们说,我们不应该用系统.out.println(),但我不理解他们的想法。可以对任何人解释一下吗?我应该用它来跟踪我的应用程序的日志?

  

的System.out.println()(或printf()的原生code)不应该被使用。的System.out和System.err重定向到/ dev / null的,让您的打印报表不会有任何明显的影响。但是,所有的字符串建筑,仍然发生了这些要求得到执行。

解决方案

这是我的问题太多。您应该使用登录类的方法。

作为Android开发者,这些都是记录工具的常用方法

  1. Log.d() - 调试
  2. Log.e() - 错误
  3. Log.i() - 信息
  4. Log.w() - 警告
  5. Log.v() - 详细

还有一个方法 Log.wtf()。就个人而言,我不知道用法:)

上述

方法有两个参数

  1. 字符串,字符串 - 这是它是一个标签和消息基本参数
  2. 字符串,字符串,可抛出 - 拥有相似的第一位。但是,如果你想记录一个异常的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

  1. Log.d() - Debug
  2. Log.e() - Error
  3. Log.i() - Information
  4. Log.w() - Warning
  5. Log.v() - Verbose

There's also a method Log.wtf(). Personally, I don't know that usage :)

Methods above have both parameters

  1. String , String - This is a basic parameter which is a TAG and Message.
  2. String, String, Throwable - Has similarity on first one. However, If you want to log an Exception or any Throwables in logcat, other than calling printStackTrace() 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, or DEBUG using System.out or System.err.
  • You can't define your own TAG, it will display System.err or System.out with your text. As equivalence

    • System.out.println("Hello!") is equivalent to Log.i("System.out","Hello!")
    • System.err.println("Hello!") is equivalent to Log.w("System.err","Hello!")

这篇关于在Android的为什么不应该使用的System.out.println()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆