public function __construct() { // build the chain of responsibility $l = new DebugLogger(Logger::DEBUG); $e = new EmailLogger(Logger::NOTICE); $s = new StderrLogger(Logger::ERR); $l->setNext($e->setNext($s)); $l->message("Entering function y.", Logger::DEBUG); // handled by DebugLogger $l->message("Step1 completed.", Logger::NOTICE); // handled by DebugLogger and EmailLogger $l->message("An error has occurred.", Logger::ERR); // handled by all three Loggers }
public function getLogger() { if ($this->_logger === null) { $this->_logger = new Zend_Log(new Zend_Log_Writer_Null()); $options = $this->getOptions(); if (!empty($options['file'])) { $this->_logger->addWriter(new Zend_Log_Writer_Stream($options['file'])); } else { $this->_logger->addWriter(new Zend_Log_Writer_Stream('D:/www/phpweb20/data/logs/debug.log')); } if (!empty($options['email'])) { $writer = new EmailLogger($options['email']); $writer->addFilter(new Zend_Log_Filter_Priority(Zend_Log::CRIT)); $this->_logger->addWriter($writer); } } return $this->_logger; }
/** * Method used to update rows in the DB. Rows are selected based * on selectCriteria and updated using values in updateValues. * <p> * Use this method for performing an update of the kind: * <p> * WHERE some_column = some value AND could_have_another_column = * another value AND so on. * * @param $selectCriteria A Criteria object containing values used in where * clause. * @param $updateValues A Criteria object containing values used in set * clause. * @param $con The Connection to use. * @return int The number of rows affected by last update statement. For most * uses there is only one update statement executed, so this number * will correspond to the number of rows affected by the call to this * method. Note that the return value does require that this information * is returned (supported) by the Creole db driver. * @throws PropelException */ public static function doUpdate(Criteria $selectCriteria, Criteria $updateValues, Connection $con) { $db = Propel::getDB($selectCriteria->getDbName()); $dbMap = Propel::getDatabaseMap($selectCriteria->getDbName()); // Get list of required tables, containing all columns $tablesColumns = $selectCriteria->getTablesColumns(); // we also need the columns for the update SQL $updateTablesColumns = $updateValues->getTablesColumns(); $affectedRows = 0; // initialize this in case the next loop has no iterations. foreach ($tablesColumns as $tableName => $columns) { $whereClause = array(); $selectParams = array(); foreach ($columns as $colName) { $sb = ""; $selectCriteria->getCriterion($colName)->appendPsTo($sb, $selectParams); $whereClause[] = $sb; } $rs = null; $stmt = null; try { $sqlSnippet = implode(" AND ", $whereClause); if ($selectCriteria->isSingleRecord()) { // Get affected records. $sql = "SELECT COUNT(*) FROM " . $tableName . " WHERE " . $sqlSnippet; $stmt = $con->prepareStatement($sql); if (isset($updateValues->debug) && class_exists('EmailLogger')) { EmailLogger::reportData($stmt); } self::populateStmtValues($stmt, $selectParams, $dbMap); $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); $rs->next(); if ($rs->getInt(1) > 1) { $rs->close(); throw new PropelException("Expected to update 1 record, multiple matched."); } $rs->close(); } $sql = "UPDATE " . $tableName . " SET "; foreach ($updateTablesColumns[$tableName] as $col) { $sql .= substr($col, strpos($col, '.') + 1) . " = ?,"; } $sql = substr($sql, 0, -1) . " WHERE " . $sqlSnippet; Propel::log($sql, Propel::LOG_DEBUG); $stmt = $con->prepareStatement($sql); if (isset($updateValues->debug) && class_exists('EmailLogger')) { EmailLogger::reportData($stmt); } // Replace '?' with the actual values self::populateStmtValues($stmt, array_merge(self::buildParams($updateTablesColumns[$tableName], $updateValues), $selectParams), $dbMap); $affectedRows = $stmt->executeUpdate(); $stmt->close(); } catch (Exception $e) { if ($rs) { $rs->close(); } if ($stmt) { $stmt->close(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException("Unable to execute UPDATE statement.", $e); } } // foreach table in the criteria return $affectedRows; }
// Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH . '/../library'), get_include_path()))); //此处采用的是phpweb20 14章采用的方法,14张将错误氛围派发前错误以及派发中错误 //派发前的错误就是此处的try catch块,派发中的错误即ErrorController类 //如果此处不使用try catch块,那么派发前的错误将直接显示在屏幕上 //使用此处的try catch块,派发前的出现错误将记录日志,并将页面转到error.html上 require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true); $logger = new Zend_Log(new Zend_Log_Writer_Null()); try { $writer = new EmailLogger($_SERVER['SERVER_ADMIN']); $writer->addFilter(new Zend_Log_Filter_Priority(Zend_Log::CRIT)); $logger->addWriter($writer); $config = new Zend_Config_Ini(APPLICATION_PATH . "/../config.ini", 'config'); Zend_Registry::set('config', $config); $logger->addWriter(new Zend_Log_Writer_Stream($config->logging->file)); $writer->setEmail($config->logging->email); Zend_Registry::set('logger', $logger); /** Zend_Application */ //require_once 'Zend/Application.php'; // Create application, bootstrap, and run $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/../settings.ini'); //把自己定义的类文件都放到library目录的Pw目录下,然后在配置文件中写入autoloadernamespaces.Pw = "Pw_"就可以自动加载了 //这里没有namespace前缀,所以要使用如下方法完成自动加载: //$application->getAutoloader()->setFallbackAutoloader(true); //Zend_Application_Module_Autoloader有一定的局限性,默认情况下他只能加载models, forms等系统定义的文件夹,自定义的不可以。