Updating file headers.

Adding CakeLog::handleError which will log errors when debug = 0.
Test cases added.
This commit is contained in:
mark_story 2009-09-07 23:59:31 -04:00
parent cac09481ac
commit d0e6a79f82
2 changed files with 71 additions and 14 deletions

View file

@ -1,6 +1,4 @@
<?php <?php
/* SVN FILE: $Id$ */
/** /**
* Logging. * Logging.
* *
@ -9,20 +7,17 @@
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org) * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* *
* Licensed under The MIT License * Licensed under The MIT License
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
* @filesource * @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake * @package cake
* @subpackage cake.cake.libs * @subpackage cake.cake.libs
* @since CakePHP(tm) v 0.2.9 * @since CakePHP(tm) v 0.2.9
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
@ -102,5 +97,49 @@ class CakeLog {
return $log->append($output); return $log->append($output);
} }
} }
/**
* An error_handler that will log errors to file using CakeLog::write();
*
* @param integer $code Code of error
* @param string $description Error description
* @param string $file File on which error occurred
* @param integer $line Line that triggered the error
* @param array $context Context
* @return void
**/
function handleError($code, $description, $file = null, $line = null, $context = null) {
if ($code === 2048 || $code === 8192) {
return;
}
switch ($code) {
case E_PARSE:
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
$level = LOG_ERROR;
break;
case E_WARNING:
case E_USER_WARNING:
case E_COMPILE_WARNING:
case E_RECOVERABLE_ERROR:
$level = LOG_WARNING;
break;
case E_NOTICE:
case E_USER_NOTICE:
$level = LOG_NOTICE;
break;
default:
return false;
break;
}
$message = '(' . $code . ') ' . $description . ' in [' . $file . ', line ' . $line . ']';
CakeLog::write($level, $message);
}
}
if (!defined('DISABLE_DEFAULT_ERROR_HANDLING')) {
set_error_handler(array('CakeLog', 'handleError'));
} }
?> ?>

View file

@ -1,6 +1,4 @@
<?php <?php
/* SVN FILE: $Id$ */
/** /**
* CakeLogTest file * CakeLogTest file
* *
@ -9,20 +7,17 @@
* PHP versions 4 and 5 * PHP versions 4 and 5
* *
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite> * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* *
* Licensed under The Open Group Test Suite License * Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice. * Redistributions of files must retain the above copyright notice.
* *
* @filesource * @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org) * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake * @package cake
* @subpackage cake.tests.cases.libs * @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 1.2.0.5432 * @since CakePHP(tm) v 1.2.0.5432
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
App::import('Core', 'Log'); App::import('Core', 'Log');
@ -54,5 +49,28 @@ class CakeLogTest extends CakeTestCase {
$this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result); $this->assertPattern('/2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 2$/', $result);
unlink(LOGS . 'error.log'); unlink(LOGS . 'error.log');
} }
/**
* Test logging with the error handler.
*
* @return void
**/
function testLoggingWithErrorHandling() {
@unlink(LOGS . 'debug.log');
Configure::write('log', E_ALL & ~E_DEPRECATED);
Configure::write('debug', 0);
$logger = new CakeLog();
Debugger::invoke($logger);
$out .= '';
$result = file(LOGS . 'debug.log');
$this->assertEqual(count($result), 1);
$this->assertPattern(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: \(8\) Undefined variable: out in \[.+ line \d{2}\]$/',
$result[0]
);
@unlink(LOGS . 'debug.log');
}
} }
?> ?>