Zend_Application: Loading Logs
zend_log, zend_application_resource, zend framework
With addition of Zend_Application in Zend Framework version 1.8, Zend is providing us with a number of classes to make building application easier. In the past I had several classes responsible for setting up my application, initializing database, setting the config, etc...
(NOTE: from ZF 1.10 onwards a Log resource is available in the library: Zend_Application_Resource_Log)
I like to use seperate log files for different uses. For this reason I've written a Logs Application Resource. It has the possibility of loading different log files, add different writers or filters. It's not 100% complete, since it needs some error handling and not all writers/filters are included. But for the rest it works fine.
I had to add following lines to my config file:
# Add my class namespace for the autoloader autoloadernamespaces.amz = "Amz_" # Add my resource path to the plugin path pluginpaths.Amz_Application_Resource = "Amz/Application/Resource" # Logs resources.logs.default.writer.1.type = stream resources.logs.default.writer.1.params.streamOrUrl = APPLICATION_PATH "/../logs/application.log" resources.logs.default.filter.1.priority = debug resources.logs.default.isDefault = true [development: production] resources.logs.default.writer.2.type = firebug
My new resource class looks like this:
<?php
class Amz_Application_Resource_Logs extends Zend_Application_Resource_ResourceAbstract
{
/**
* Logs
*
* @var array
*/
protected $_logs = array();
/**
* Default adapter
*
* @var boolean
*/
protected $_defaultLog = null;
/**
* Retrieve initialized Log
*
* @return null|Zend_Log
*/
public function getLog($log = null)
{
// check if the Log is valid
$log = $this->isValidLog($log, true);
if (is_null($log) && is_null($log = $this->_defaultLog)) {
return null;
}
$options = $this->getOptions();
if ((!isset($this->_logs[$log]) || (null === $this->_logs[$log]))
&& (in_array($log, array_keys($options)))
) {
$this->_logs[$log] = $this->_initLog($log);
}
return $this->_logs[$log];
}
/**
* Defined by Zend_Application_Resource_IResource
*
* @return void
*/
public function init()
{
if (is_null($this->_defaultLog)) {
$defaultLog = null;
foreach ($this->_options as $key=>$logOptions) {
if (null !== ($log = $this->getLog($key))) {
if ($this->isDefaultLog($key) || is_null($defaultLog)) {
$defaultLog = $key;
}
}
}
if (!is_null($defaultLog)) {
$this->_defaultLog = $defaultLog;
Zend_Registry::set('log', $this->getLog($defaultLog));
}
}
return $this->_logs;
}
/**
* Check if a log key is valid
*
* @param string $log
* @param boolean $revertToDefaultLog
* @return string
*/
public function isValidLog($log = null, $revertToDefaultLog = false)
{
if (is_null($log)) {
$revertToDefaultLog = true;
}
if (!in_array($log, array_keys($this->_options))) {
if ($revertToDefaultLog) {
$log = $this->_defaultLog;
} else {
throw new Zend_Application_Resource_Exception('Invalid log specified');
}
}
return $log;
}
/**
* Initialize log
*
* @param string $key
* @return Zend_Log
*/
protected function _initLog($key)
{
$logOptions = $this->_options[$key];
if (isset($logOptions['writer'])) {
$log = new Zend_Log();
foreach ($logOptions['writer'] as $writerOptions) {
$writer = null;
switch (strtolower(trim($writerOptions['type']))) {
case 'db':
$db = $writerOptions['params']['db'];
$table = $writerOptions['params']['db'];
$columnMap = $writerOptions['params']['db'];
$writer = new Zend_Log_Writer_Db($db, $table, $columnMap);
break;
case 'firebug':
$writer = new Zend_Log_Writer_Firebug();
break;
case 'mock':
$writer = new Zend_Log_Writer_Mock();
break;
case 'null':
$writer = new Zend_Log_Writer_Null();
break;
case 'stream':
$streamOrUrl = $writerOptions['params']['streamOrUrl'];
$mode = isset($writerOptions['params']['mode']) ? $writerOptions['params']['mode'] : 'a';
$writer = new Zend_Log_Writer_Stream($streamOrUrl, $mode);
break;
default:
$writer = null;
}
if (!is_null($writer)) {
$log->addWriter($writer);
}
}
if (isset($logOptions['filter'])) {
foreach ($logOptions['filter'] as $filter) {
if (isset($filter['priority'])) {
$priority = $filter['priority'];
if (!is_integer($priority)) {
$c = 'Zend_Log::' . strtoupper(trim($priority));
$priority = constant($c);
}
$logFilter = new Zend_Log_Filter_Priority($priority);
Zend_Log::addFilter($logFilter);
} elseif (isset($filter['message'])) {
$message = $filter['message'];
$logFilter = new Zend_Log_Filter_Message($message);
Zend_Log::addFilter($logFilter);
}
}
}
}
return $log;
}
/**
* Is this log the default log?
*
* @return void
*/
public function isDefaultLog($log)
{
$log = $this->isValidLog($log);
if (isset($this->_options[$log]['isDefault'])) {
return $this->_options[$log]['isDefault'];
}
return false;
}
}
And last but not least, here's my Bootstrap file for loading and using the logfile:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function run()
{
$this->bootstrap('logs');
$logs = $this->getPluginResource('logs');
Zend_Registry::set('log', $logs->getLog());
$this->bootstrap('frontController');
$this->frontController->dispatch();
}
}
Enjoy!






+32 475 62.42.64