title: Log caught exception with stack trace
body: <p>If I don't catch an exception in PHP, I get a helpful error message in my <code>error.log</code> file with a stack trace. For example, if I run:</p>
  
  <pre><code>&lt;?php
  
    function foo() {
      throw new Exception('Oh no!');
    } 
  
    foo();
  
  ?&gt;
  </code></pre>
  
  <p>then I get this written to my logs:</p>
  
  <blockquote>
    <p>[Wed Mar 06 10:35:32 2013] [error] [client 86.146.145.175] PHP Fatal
    error:  Uncaught exception 'Exception' with message 'Oh no!' in
    /var/www/test.php:4\nStack trace:\n#0 /var/www/test.php(7):
    foo()\n#1 {main}\n  thrown in /var/www/test.php on line 4</p>
  </blockquote>
  
  <p>Sometimes I'd like to catch the exception but still log that detail. I'm imagining something like:</p>
  
  <pre><code>&lt;?php
  
    function foo() {
      throw new Exception('Oh no!');
    } 
  
    try {
        foo();
    } catch (Exception $e) {
        log_exception($e);
    }
  
  ?&gt;
  </code></pre>
  
  <p>where <code>log_exception</code> will write to the error log something in basically the same format as what gets automatically written for an uncaught exception - perhaps literally identical besides having <code>Caught exception</code> instead of <code>PHP Fatal error: Uncaught exception</code>.</p>
  
  <p>Is there a built-in function to log exception info like this, or to capture it to a string? I'm imagining something analagous to <code>traceback.format_exc()</code> in Python.</p>