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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...