Creating a Datestamped Log File using the Standard Log Resource Plugin
Wednesday, March 10, 2010

In a previous post, I looked at creating a custom resource plugin to create a logger. Since I wrote that code, a standard resource plugin to perform this function has been created. At first sight the standard plugin is inferior, in that the custom plugin allows for the insertion of a date into the log filename, which allows a new log to be created every day, while the standard plugin requires a fixed filename specified in the configuration file. However, I have worked out a simple mechanism for adding a date stamp to the filename, which I shall share in this article.

The key things is that it is possible to use constants in the configuration file. The first step is therefore to define a constant to represent the current date stamp in htdocs/index.php:

// Define current date
define('DATESTAMP', date('Y-m-d'));

This should come after the definitions of APPLICATION_PATH and APPLICATION_ENV (see the earlier post on index.php).

This constant can now be used in the configuration file (application/configs/application.ini) to declare the log filename, along with the other necessary settings:

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = 
  APPLICATION_PATH "/logs/log_" DATESTAMP
resources.log.stream.writerParams.mode = "a"

To use the logger, you can fetch the object created like this from within the controller:

$bootstrap = $this->getInvokeArg('bootstrap');
$log = $bootstrap->getResource('log');

Then use the logger in the normal fashion, e.g.

$log->err('Page not found');
Posted by James at 11:40 pm   0 comments

Leave a Reply