Nguyen  Minh Binh Nguyen Minh Binh - 1 year ago 53
Android Question

Why shouldn't I use System.out.println() in android

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.

Answer Source

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
  6. - What a Terrible Failure: Report an exception that should never happen.

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!")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download