Android - print a full backtrace exception for a log

I have a try / catch block that throws an exception and I would like to see the exception information in the Android device log.

I read the mobile device log using this command from my development computer:

/home/dan/android-sdk-linux_x86/tools/adb shell logcat 

I tried this first:

 try { // code buggy code } catch (Exception e) { e.printStackTrace(); } 

but it doesn’t print anything in the magazine. Sorry because it helped a lot.

The best I have achieved:

 try { // code buggy code } catch (Exception e) { Log.e("MYAPP", "exception: " + e.getMessage()); Log.e("MYAPP", "exception: " + e.toString()); } 

Better than nothing, but not very satisfactory.

Do you know how to print a full back trace in a log?

Thank.

+87
android
Dec 03 2018-10-12T00:
source share
8 answers
 try { // code that might throw an exception } catch (Exception e) { Log.e("MYAPP", "exception", e); } 

In more detail with additional information.

(Since this is the oldest question about this.)

Android log methods with three arguments will print a stack trace for Exception , which is provided as the third parameter. for example

 Log.d(String tag, String msg, Throwable tr) 

where tr is the exception.

According to this comment, these log methods "use the getStackTraceString() ... method backstage" to do this.

+155
Dec 03 2018-10-12T00:
source share

This helper function also works well, since Exception is also Throwable.

  try{ //bugtastic code here } catch (Exception e) { Log.e(TAG, "Exception: "+Log.getStackTraceString(e)); } 
+45
Sep 29 '11 at 15:44
source share
 catch (Exception e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream stream = new PrintStream( baos ); e.printStackTrace(stream); stream.flush(); Log.e("MYAPP", new String( baos.toByteArray() ); } 

Or ... I know ... what EboMike said.

+8
Dec 03 2018-10-12T00: 00Z
source share
 public String getStackTrace(Exception e){ StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); return sw.toString(); } 
+3
Aug 10 '15 at 18:30
source share

e.printStackTrace () prints it to me. I don’t think you are managing the logarithm correctly. Do not run it in the shell, just run

/home/dan/android-sdk-linux_x86/tools/adb logcat

+2
Dec 03 2018-10-12T00:
source share

The default output and error output is directed to / dev / null by default, so they are all lost. If you want to write this output, you need to follow the instructions "View stdout and stderr" shown here

+1
Nov 20 '11 at 12:09
source share
 try{ ... } catch (Exception e) { Log.e(e.getClass().getName(), e.getMessage(), e.getCause()); } 
+1
Dec 18 '15 at 3:04 on
source share

In the context of Android, I had to pass the exception to the string:

 try { url = new URL(REGISTRATION_PATH); urlConnection = (HttpURLConnection) url.openConnection(); } catch(MalformedURLException e) { Log.i("MALFORMED URL", String.valueOf(e)); } catch(IOException e) { Log.i("IOException", String.valueOf(e)); } 
0
Mar 09 '17 at 20:51
source share



All Articles