Bootstrapping the Application: a Custom Log Resource Plugin


So far, we have been looking at how to use several of the standard resource plugins that come with Zend_Application in order to initialise application resources. It is possible, indeed encouraged, to create your own custom resource plugins to handle the initialisation of resources that fall outside their scope.

One very useful class is Zend_Log, which provides a handy mechanism for logging application errors or user activity. I have created a custom resource plugin called Default_Resource_Log, which handles the initialisation of a Zend_Log, and storage of it in the registry for future retrieval.

To use custom resource plugins, you need to register a plugin prefix and associated directory. In the case of the sample application, the prefix is Default_Resource and the directory is resources; these are added to the configuration file like this to perform the registration:

pluginPaths.Default_Resource = APPLICATION_PATH "/resources"

The plugin itself is implemented in a file called Log.php, which is in the resources directory. Note that although the class is called Default_Resource_Log, the prefix is excluded when naming the file in which it is contained.

The basic class skeleton is as follows:

class Default_Resource_Log 
  extends Zend_Application_Resource_ResourceAbstract
{
  // Add class methods and variables here
}

Note that Zend_Application_Resource_ResourceAbstract is the base class for resource plugins.

It has a member variable used to store the Zend_Log:

protected $_log;

Zend_Application will call the init method of the class to retrieve the resource it initialises. The class uses the singleton pattern to implement this:

public function init()
{
  return $this->getLog();
}

public function getLog()
{
  if (null === $this->_log)
  {
    $options = $this->getOptions();
    $file = str_replace('%d', date('Y-m-d'), $options['file']);
    $log = new Zend_Log(new Zend_Log_Writer_Stream($file));
    Zend_Registry::set('Zend_Log', $log);
    $this->_log = $log;
  }

  return $this->_log;
}

If Zend_Log has already been created, it returns the reference stored in $_log; otherwise, it creates the Zend_Log, stores a reference to it and returns that.

The reference to Zend_Log is also stored in the registry under the key Zend_Log; this allows it to be retrieved later by:

$log = Zend_Registry::get('Zend_Log');

Note the call to getOptions; this allows the resource plugin to retrieve any settings in the configuration file that apply to it. Here, the following line has been added to the configuration:

resources.log.file = APPLICATION_PATH "/logs/application-%d.log"

This sets the location of the log file. Note that the resource plugin will look for the string %d in the name and replace it with the current date; this leads to a different log file being created every day.

Note that it is necessary to add at least one line relating to the resource plugin to the configuration file in order to have it initialised; if there were no parameters, you would just add:

resources.log[] =
Source: http://zf.gm-ram.com/posts/bootstrapping-the-application-a-custom-log-resource-plugin/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s