Skip to content

Commit

Permalink
Updating file headers.
Browse files Browse the repository at this point in the history
Adding CakeLog::handleError which will log errors when debug = 0.
Test cases added.
  • Loading branch information
markstory committed Sep 8, 2009
1 parent cac0948 commit d0e6a79
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
53 changes: 46 additions & 7 deletions cake/libs/cake_log.php
@@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */

/**
* Logging.
*
Expand All @@ -9,20 +7,17 @@
* PHP versions 4 and 5
*
* 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
* Redistributions of files must retain the above copyright notice.
*
* @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
* @package cake
* @subpackage cake.cake.libs
* @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
*/

Expand Down Expand Up @@ -102,5 +97,49 @@ function write($type, $msg) {
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'));
}
?>
32 changes: 25 additions & 7 deletions cake/tests/cases/libs/cake_log.test.php
@@ -1,6 +1,4 @@
<?php
/* SVN FILE: $Id$ */

/**
* CakeLogTest file
*
Expand All @@ -9,20 +7,17 @@
* PHP versions 4 and 5
*
* 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
* Redistributions of files must retain the above copyright notice.
*
* @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
* @package cake
* @subpackage cake.tests.cases.libs
* @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
*/
App::import('Core', 'Log');
Expand Down Expand Up @@ -54,5 +49,28 @@ function testLogFileWriting() {
$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');
}

/**
* 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');
}
}
?>

0 comments on commit d0e6a79

Please sign in to comment.