Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^240) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
example from the previous section with Zend_Log. First, place the code shown in Listing 16-6 in
your bootstrap file, at any location after the framework is loaded (registerAutoload()), but
before your front controller is run(). This location is the same for any other additional functionality
added to the bootstrap file.
Listing 16-6. Creating a Logger (in index.php)
//Create a log
$log = new Zend_Log();
//Filter out noise [optional]
$log->addFilter(new Zend_Log_Filter_Priority(Zend_Log::ERR));
//Create a writer
$logWriter = new Zend_Log_Writer_Stream(APP_PATH. '/application.log');
//Create a formatter [optional]
$logFormat = '%timestamp% %priorityName% %message%'. "\n";
$logWriter->setFormatter(new Zend_Log_Formatter_Simple($logFormat));
//Add the writer to the log
$log->addWriter($logWriter);
//Store it in the registry for easy access
Zend_Registry::set('log', $log);
Listing 16-6 creates an application.log file in the application’s root folder. For this to work,
the directory must be writable by the web server user, which may be www-data, nobody, or apache2,
depending on your distribution. The log filters out any messages with a priority lower than ERR
and formats the error message in a human-readable format. The formatter and filter are both
optional steps.
■Note Eight default message priorities ship with the Zend Framework: EMERG, ALERT, CRIT, ERR, WARN,
NOTICE, INFO, and DEBUG.
At this point, your logger is set up, but you have yet to log any messages. Listing 16-7
demonstrates a possible integration between the ErrorController and this log.
Listing 16-7. Logging with the ErrorController (ErrorController.php)
class ErrorController extends Zend_Controller_Action {
public function errorAction() {
$request = $this->getRequest();
$errorHandler = $request->getParam('error_handler');
McArthur_819-9C16.fm Page 240 Friday, February 29, 2008 5:07 PM

Free download pdf