Why Shouldn't I Use System.Out.Println() in Android

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

You should use the android.util.Log class.

Here's a description of what the Log class does:

API for sending log output.

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.

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.

These are the available methods of the Log class:

  1. Log.d() - Send a DEBUG log message.
  2. Log.e() - Send an ERROR log message.
  3. Log.i() - Send an INFO log message.
  4. Log.v() - Send a VERBOSE log message.
  5. Log.w() - Send a WARN log message.
  6. Log.wtf() - What a Terrible Failure: Report an exception that should never happen.

The methods above (with the exception of Log.w and Log.wtf which have 3 possible patterns of arguments) require the following arguments:

  1. String tag, String msg:

    tag: Used to identify the source of a log message. This value may be null.

    msg: The message you would like logged. This value may be null.

  2. 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.

  3. (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: Going straight to answer your question: println() of System.out and System.err will still be 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. For instance:

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

Why doesn't System.out.println work in Android?

Correction:


On the emulator and most devices System.out.println gets redirected to LogCat and printed using Log.i(). This may not be true on very old or custom Android versions.

Original:


There is no console to send the messages to so the System.out.println messages get lost. In the same way this happens when you run a "traditional" Java application with javaw.

Instead, you can use the Android Log class:

Log.d("MyApp","I am here");

You can then view the log either in the Logcat view in Eclipse, or by running the following command:

adb logcat

It's good to get in to the habit of looking at logcat output as that is also where the Stack Traces of any uncaught Exceptions are displayed.

The first Entry to every logging call is the log tag which identifies the source of the log message. This is helpful as you can filter the output of the log to show just your messages. To make sure that you're consistent with your log tag it's probably best to define it once as a static final String somewhere.

Log.d(MyActivity.LOG_TAG,"Application started");

There are five one-letter methods in Log corresponding to the following levels:

  • e() - Error
  • w() - Warning
  • i() - Information
  • d() - Debug
  • v() - Verbose
  • wtf() - What a Terrible Failure

The documentation says the following about the levels:

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.

string & system.out.println and other functions doesn't work

EDIT: Classnames starts with a capital letter on Java

System.out.println();

In this case you can also use the Logcat console,

Log.v(TAG, your_value_var);

I give you anothers comments and advices.

First of all I think you should download a Java language paper from the beginning, there you would see and you could clarify all these doubts and you could solve all the language problems.

Secondly I would advise you to download the latest version of Android Studio, the one you are using is a bit old, maybe with bugs and others, Android Studio 3, made many changes for our benefit.

And finally I advise you here Stackoverflow when you ask a question is because we have tried to solve the problem in many ways by other ways and we have tried many times, things like these are not wonder here, but we see that you are very new, and that's how we all started.

Welcome to the Community.

I hope it helps you improve.

Do not use System.out.println in server side code

System.out.println is an IO-operation and therefor is time consuming.
The Problem with using it in your code is, that your program will wait until the println has finished. This may not be a problem with small sites but as soon as you get load or many iterations, you'll feel the pain.

The better approach is to use a logging framework.
They use a message queue and write only if no other output is going on.

And another benefit is that you can configure separate log files for different purposes.
Something your Ops team will love you for.

Read more here:

  • http://logging.apache.org/log4j/1.2/manual.html
  • Logger vs. System.out.println

Does System.out.println() effect the code efficiency ?

System.out implementation contains a synchronized block on the output stream.

From PrintStream.java :

      /**
* Prints a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/

public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}

Putting System.out.println a lot will make the entire project to run almost single-threaded, because all thread will waiting for synchronization lock, and make your application begin to crawl.

This means your superior is right.

Alternatively, Use logging framework like log4j instead. You can still configure your log4j to still output to System.out.println by just minor changes in the appender configuration.



Related Topics



Leave a reply



Submit