3

I'm looking for a Log4J Layout/Formatter that helps me prune stack-traces in exceptions a little better than the defaults. That the stack-execution is somewhere below main() is quite obvious and unnecessary for me to know, and that the exception occurred deeply within some other library is really nothing I can do too much about.

What I would like is a Layout that trims the stack-trace to, say the last 5 method-calls of method within my own code, identified by containing jar-file, package or something else.

Is there something along these lines, or do I have to write some magic myself?

Rawler
  • 1,480
  • 1
  • 11
  • 23

5 Answers5

2

I asked a similar question (about completely suppressing the stack trace a while back.

Unfortunately, there is no setting for that, you need to subclass PatternLayout to do it.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

The "ex" or "exception" conversion word in logback-classic (log4j's successor) supports printing the specified number of stack trace lines. The "ex" conversion word is documented with the rest of conversion words. You need to scroll down a little.

If you need further information on this topic, please contact the logback-user mailing list.

Ceki
  • 26,753
  • 7
  • 62
  • 71
1

I tried looking for this as well but didn't find anything satisfactory so I wrote up a custom ThrowableRenderer which prunes out extraneous stack frames. I've open sourced the code for others to use. Check out my blog post on it; you can find a link to the code there.

Zubair Khan
  • 46
  • 1
  • 4
  • Thank you. Without having tested it, it seems like what I was looking for back then. Unfortunately, I no longer have a need for it, but I'm accepting this answer to help whomever comes looking for it. – Rawler Feb 22 '12 at 14:31
0

There are three ways to do this:

  1. Find a library or code that someone else did
  2. Write it yourself (extend/implement)
  3. Use the default Layout, but prune the exception's stack before passing it to logger
Azder
  • 4,698
  • 7
  • 37
  • 57
  • Thank you, I know that I can do it myself. I was asking whether there already are something that (partially?) solves this. The inflection-parts are a bit over my head. – Rawler May 07 '09 at 22:44
0

You can write a custom Appender that has the special logic you need. That might be a good way to go.

duffymo
  • 305,152
  • 44
  • 369
  • 561