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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex