Merge branch '1.3-logging' into 1.3-misc

This commit is contained in:
mark_story 2009-09-09 11:08:18 -04:00
commit 4766f62029
5 changed files with 114 additions and 35 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

@ -42,7 +42,7 @@ class Configure extends Object {
* @var integer * @var integer
* @access public * @access public
*/ */
var $debug = null; var $debug = 0;
/** /**
* Returns a singleton instance of the Configure class. * Returns a singleton instance of the Configure class.
@ -106,25 +106,31 @@ class Configure extends Object {
} }
if (isset($config['debug'])) { if (isset($config['debug'])) {
$reporting = 0;
if ($_this->debug) { if ($_this->debug) {
error_reporting(E_ALL & ~E_DEPRECATED);
if (function_exists('ini_set')) {
ini_set('display_errors', 1);
}
if (!class_exists('Debugger')) { if (!class_exists('Debugger')) {
require LIBS . 'debugger.php'; require LIBS . 'debugger.php';
} }
$reporting = E_ALL & ~E_DEPRECATED;
if (function_exists('ini_set')) {
ini_set('display_errors', 1);
}
} elseif (function_exists('ini_set')) {
ini_set('display_errors', 0);
}
if (isset($_this->log) && $_this->log) {
if (!class_exists('CakeLog')) { if (!class_exists('CakeLog')) {
require LIBS . 'cake_log.php'; require LIBS . 'cake_log.php';
} }
Configure::write('log', LOG_NOTICE); if (is_integer($_this->log) && !$_this->debug) {
$reporting = $_this->log;
} else { } else {
error_reporting(0); $reporting = E_ALL & ~E_DEPRECATED;
Configure::write('log', LOG_NOTICE);
} }
} }
error_reporting($reporting);
}
} }
/** /**
@ -143,13 +149,6 @@ class Configure extends Object {
$_this =& Configure::getInstance(); $_this =& Configure::getInstance();
if ($var === 'debug') { if ($var === 'debug') {
if (!isset($_this->debug)) {
if (defined('DEBUG')) {
$_this->debug = DEBUG;
} else {
$_this->debug = 0;
}
}
return $_this->debug; return $_this->debug;
} }

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

View file

@ -144,13 +144,15 @@ class ConfigureTest extends CakeTestCase {
* @return void * @return void
**/ **/
function testSetErrorReportingLevel() { function testSetErrorReportingLevel() {
Configure::write('log', false);
Configure::write('debug', 0); Configure::write('debug', 0);
$result = ini_get('error_reporting'); $result = ini_get('error_reporting');
$this->assertEqual($result, 0); $this->assertEqual($result, 0);
Configure::write('debug', 2); Configure::write('debug', 2);
$result = ini_get('error_reporting'); $result = ini_get('error_reporting');
$this->assertEqual($result, E_ALL); $this->assertEqual($result, E_ALL & ~E_DEPRECATED);
$result = ini_get('display_errors'); $result = ini_get('display_errors');
$this->assertEqual($result, 1); $this->assertEqual($result, 1);
@ -160,6 +162,28 @@ class ConfigureTest extends CakeTestCase {
$this->assertEqual($result, 0); $this->assertEqual($result, 0);
} }
/**
* test that log and debug configure values interact well.
*
* @return void
**/
function testInteractionOfDebugAndLog() {
Configure::write('log', false);
Configure::write('debug', 0);
$this->assertEqual(ini_get('error_reporting'), 0);
$this->assertEqual(ini_get('display_errors'), 0);
Configure::write('log', E_WARNING);
Configure::write('debug', 0);
$this->assertEqual(ini_get('error_reporting'), E_WARNING);
$this->assertEqual(ini_get('display_errors'), 0);
Configure::write('debug', 2);
$this->assertEqual(ini_get('error_reporting'), E_ALL & ~E_DEPRECATED);
$this->assertEqual(ini_get('display_errors'), 1);
}
/** /**
* testDelete method * testDelete method
* *

View file

@ -28,8 +28,7 @@
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1); define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
} }
App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysql')); App::import('Model', array('Model', 'DataSource', 'DboSource', 'DboMysql', 'App'));
App::import('Model', 'App');
require_once dirname(dirname(__FILE__)) . DS . 'models.php'; require_once dirname(dirname(__FILE__)) . DS . 'models.php';
/** /**