note 98201 added to function.set-exception-handler
| From: | pinkgothic at gmail dot com | Date: | Tue, 01 Jun 2010 09:30:33 +0000 |
| Subject: | note 98201 added to function.set-exception-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169770@lists.php.net to get a copy of this message | ||
If you're handling sensitive data and you don't want exceptions logging details such as
variable contents when you throw them, you may find yourself frustratedly looking for the bits and
pieces that make up a normal stack trace output, so you can retain its legibility but just alter a
few things. In that case, this may help you:
<?php
function exceptionHandler($exception) {
// these are our templates
$traceline = "#%s %s(%s): %s(%s)";
$msg = "PHP Fatal error: Uncaught exception '%s' with message '%s' in
%s:%s\nStack trace:\n%s\n thrown in %s on line %s";
// alter your trace as you please, here
$trace = $exception->getTrace();
foreach ($trace as $key => $stackPoint) {
// I'm converting arguments to their type
// (prevents passwords from ever getting logged as anything other than 'string')
$trace[$key]['args'] = array_map('gettype',
$trace[$key]['args']);
}
// build your tracelines
$result = array();
foreach ($trace as $key => $stackPoint) {
$result[] = sprintf(
$traceline,
$key,
$stackPoint['file'],
$stackPoint['line'],
$stackPoint['function'],
implode(', ', $stackPoint['args'])
);
}
// trace always ends with {main}
$result[] = '#' . ++$key . ' {main}';
// write tracelines into main template
$msg = sprintf(
$msg,
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
implode("\n", $result),
$exception->getFile(),
$exception->getLine()
);
// log or echo as you please
error_log($msg);
}
?>
If you're not a fan of sprintf() or the duplicate $exception->getFile() and
$exception->getLine() calls you can of course replace that as you like - consider this a mere
compilation of the parts.
----
Server IP: 69.147.83.197
Probable Submitter: 217.92.147.99
----
Manual Page -- http://www.php.net/manual/en/function.set-exception-handler.php
Edit -- https://master.php.net/note/edit/98201
Del: integrated -- https://master.php.net/note/delete/98201/integrated
Del: useless -- https://master.php.net/note/delete/98201/useless
Del: bad code -- https://master.php.net/note/delete/98201/bad+code
Del: spam -- https://master.php.net/note/delete/98201/spam
Del: non-english -- https://master.php.net/note/delete/98201/non-english
Del: in docs -- https://master.php.net/note/delete/98201/in+docs
Del: other reasons-- https://master.php.net/note/delete/98201
Reject -- https://master.php.net/note/reject/98201
Search -- https://master.php.net/manage/user-notes.php