title: Best way of logging exceptions when tests fail (e.g. using a junit rule)
body: <p>When I'm running a complete test suite, it would be helpful if exceptions that caused a test to fail would appear in my (SLF4J-)log. What is the best method to achieve this?</p>
  
  <h3> What I would like </h3>
  
  <p>is a junit4 rule that handles exception logging for me. The code </p>
  
  <pre><code>@Rule
  public TestRule logException = new TestWatcher() {
      @Override
      public void failed(Description d) {
          catch (Exception e) {
              logger.error("Test ({}) failed because of exception {}", d, e);
              throw e;
          }
      }
  }
  </code></pre>
  
  <p>of course does not work, since I can only catch exceptions out of a try block. Is there a workaround to somehow achieve this in a similarly simple and general way?</p>
  
  <hr>
  
  <h3>BTW, what I'm doing right now</h3>
  
  <p>is logging the exception the moment it is created. But it would be nicer to log exceptions at the interface between caller and the library, so in my case in the test case. Not logging when the exceptions are created would also guarantee that they don't show up multiple times when the caller decides to log them. </p>