Skip to main content

How can I hide long class paths in stack traces to make them readable?



Often stack traces can get so verbose from long class paths that they are very painful to read. Here's an example:







1) No implementation for java.util.Set<

com.mydomain.myapp.android.activities.catbrowser.generalizedbrowser.listview.

helpers.databaseitem.itemmanipulators.ItemManipulator<

com.mydomain.myapp.flash.Cat>> annotated with

@com.google.inject.assistedinject.Assisted(value=) was bound.

while locating

java.util.Set<

com.mydomain.myapp.android.activities.catbrowser.generalizedbrowser.listview.

helpers.databaseitem.itemmanipulators.ItemManipulator<

com.mydomain.myapp.flash.Cat>> annotated with

@com.google.inject.assistedinject.Assisted(value=)







...





If I could trim the class path, only showing class names and methods, it would look like this:







1) No implementation for

Set<ItemManipulator<Cat>> annotated with @Assisted(value=) was bound.

while locating Set<ItemManipulator<Cat>> annotated with @Assisted(value=)







...





I first asked this as a Guice-specific question , but realized it applies to stack traces in general. Is there any way to configure Java or Eclipse to do this natively? If not, is there a plugin or even external tool to accomplish this?


Comments

  1. You can set the default UncaughtExceptionHandler and modify the stack trace before printing to System.err. You may have to play around with the regex, but this will work:

    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos);
    e.printStackTrace(ps);
    String withoutClasspaths = baos.toString().replaceAll("(\\w+\\.){2,}(\\w*)", "$2");
    System.err.println(withoutClasspaths);
    }
    });

    ReplyDelete
  2. To produce more readable traces, paste the stack trace into Notepad++ and the following regular expression. The same expression could also be used in a scripting language.

    I paste the trace into Notepad++ and use the following search and replace settings.

    Search pattern: \w[a-z\d_\.]+\.([A-Z][A-Za-z\d_]*)

    Replace with: \1

    Search Settings: match case enabled, Regular expression search mode.

    ReplyDelete
  3. If you use logback, then you can use layout to output logs in any way you like
    http://logback.qos.ch/manual/layouts.html.

    Outputting a class like java.lang.String as j.l.String is quite common in stack traces

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.