65

If I don't catch an exception in PHP, I get a helpful error message in my error.log file with a stack trace. For example, if I run:

<?php

  function foo() {
    throw new Exception('Oh no!');
  } 

  foo();

?>

then I get this written to my logs:

[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

Sometimes I'd like to catch the exception but still log that detail. I'm imagining something like:

<?php

  function foo() {
    throw new Exception('Oh no!');
  } 

  try {
      foo();
  } catch (Exception $e) {
      log_exception($e);
  }

?>

where log_exception 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 Caught exception instead of PHP Fatal error: Uncaught exception.

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 traceback.format_exc() in Python.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459

4 Answers4

94
error_log($e);

does what you want. It logs exactly the same thing that would have been logged if you didn't catch the exception, minus the word 'Uncaught' at the beginning. It does this because that's what the Exception class's __toString() magic method returns.

You can do this in a catch block:

try {
    foo();
} catch (Exception $e) {
    error_log("Caught $e");
}

Or in the exception handler:

set_exception_handler(function($exception) {
    error_log($exception);
    error_page("Something went wrong!");
});
Jay K
  • 2,081
  • 1
  • 17
  • 19
10

You can use the methods from PHP's base Exception class.

Use getMessage to get the message Oh no! and use getTraceAsString to get a formatted trace.

Community
  • 1
  • 1
enricog
  • 4,226
  • 5
  • 35
  • 54
8

We use Monolog to do the logging in our application. Monolog has a formatter that can print stack traces. To log exceptions with traces, we use a LineFormatter and call includeStacktraces() on it. (code below)

$handler = new \Monolog\Handler\StreamHandler(STDOUT);

$lineFormatter = new \Monolog\Formatter\LineFormatter();
$lineFormatter->includeStacktraces();

$handler->setFormatter($lineFormatter);

$logger = new \Monolog\Logger('root', [$handler]);

try {
    //do some throwing
} catch (Exception $e) {
    //do some logging, add exception to context
    $logger->error($e->getMessage(), ['exception' => $e]);
}
Eelke van den Bos
  • 1,423
  • 1
  • 13
  • 18
-3

You can use http://php.net/manual/en/function.set-exception-handler.php to register a callback function which would get the message from $e->getMessage(); and dump it to file.

varuog
  • 3,031
  • 5
  • 28
  • 56
  • 6
    The description says that this sets a handler for uncaught exceptions, which is precisely not the case I'm asking about. – Mark Amery Mar 06 '13 at 23:21