Share the post "Android: Showing System.out messages to console"
In Java, we use System.out or System.err.println() to show messages in the console. However, these don’t work in Android. These methods do not produce errors on compilation and does nothing during runtime. Chris Glover made this nifty class to actually be able to use System.out and System.err in your programming needs.
package ***;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.util.Log;
public class LogTool extends OutputStream {
private ByteArrayOutputStream bos = new ByteArrayOutputStream();
private String name;
public LogTool(String name) {
this.name = name;
}
@Override
public void write(int b) throws IOException {
if (b == (int)'\n') {
String s = new String(this.bos.toByteArray());
Log.v(this.name, s);
this.bos = new ByteArrayOutputStream();
} else {
this.bos.write(b);
}
}
}
At the startup of your code, use these lines
System.setErr(new PrintStream(new LogTool("System.err")));
System.setOut(new PrintStream(new LogTool("System.out")));
Then you can use System.out and System.err.println(). If you want to convert Exception messages generated from the printStackTrace() method of Exception classes, use this Exception to String method converter that I have.
public static String getExceptionStackTraceAsString(Exception exception) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
return sw.toString();
}

Hi… I was very interested to try this, since I am an Android newbie, but it doesn’t seem to work for me. I see no expected output in the Android emulator.
In my code is:
System.setErr(new PrintStream(new LogTool(“System.err”)));
System.setOut(new PrintStream(new LogTool(“System.out”)));
System.out.println(“Hello from System.out.println”);
System.out.print(“Another string” + ‘\n’);
System.err.println(“Hello from System.err”);
Next step: run this in the debugger? Not sure how much that will help.
hi bruce, i stopped using android a long time ago, sorry can’t help since i also uninstalled my eclipse gui program.
when i tried that, it did work for me.
That was fast! Well, it turns out I can see these messages using LogCat. So that gets me started. Thanks for the code…
welcome man
evittez de commander des articles sur consoles mobiles c’est des escrocs
Tres bon post. Assurance malus
I don’t agree with everything in this write-up, but you do make some very good points. Im very interested in this matter and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment. Feel free to check out my website sometime and let me know what you think.