title: how to print only top few stacks in a stacktrace in Java
body: <p>We are using Log4j and Slf4j. In the application log, sometimes we need the stacktrace in order to tell what is buggy, but the whole stacktrace may be so verbose that it overwhelms the log and hurts performance. I wonder if there's a way to do something like the following:</p>
  <pre><code>import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  private static final Logger LOG = LoggerFactory.getLogger(FOO.class);
  
  catch (Exception e) {
    LOG.error(&quot;Error in handling the request&quot;, e.getTopStacks(10));
  }
  </code></pre>
  <p>I think this is better than logging the full exception because the stacktrace especially thrown from RPC handling (calls and wrappers in the RPC framework, you know) can take 20-30 lines and even more. And it's better than logging only <code>e.getMessage()</code> because the application is not written by one person. It's hard to make sure all exceptions have a meaningful message.</p>
  <p>And recommendations like existing wheels from popular common utility libraries like guava or apache.commons? Thanks in advance!</p>