Ransford Okpoti's Blog

March 18, 2012

Creating Dynamic Log Files In Zend Framework (ZF)

Filed under: PHP — Tags: , — ranskills @ 11:58 am

The motivation for wanting to do something like this could be any of the following:

  • You want to have separate logs depending on the context (i.e. application, testing, development, etc) in which the application is being run and you do not want to inherit a context and override some values in another context in the configuration file.
  • If your application builds huge log files and, as a result, you want to partition the logs into, say, daily, weekly, monthly, yearly, etc. logs

Below is a typical configuration in the application.ini file.

[application]
; Loggers
resources.log.stream.writerName             = "Stream"
resources.log.stream.writerParams.stream    = APPLICATION_PATH "/../data/logs/application.log"
resources.log.stream.writerParams.mode      = "a"
resources.log.stream.filterName             = "Priority"
resources.log.stream.filterParams.priority  = 4

The problem can easily be overcome by adding the highlighted lines to the configuration file, these settings will be used in the bootstrap file to create the log files.

; Loggers
resources.log.stream.writerName             = "Stream"
resources.log.stream.writerParams.stream    = APPLICATION_PATH "/../data/logs/application"
resources.log.stream.writerParams.mode      = "a"
resources.log.stream.filterName             = "Priority"
resources.log.stream.filterParams.priority  = 4

log.path               = APPLICATION_PATH "/../data/logs"
log.partitionStrategy  = "context"
log.partitionFrequency = "weekly"

New Entries Explained

log.path
Specifies the location/directory where the files would be created. NOTE: Linux users have to make sure the appropriate read/write permissions are set.
log.partitionStrategy
Defaults to context which denotes that log files will be prefixed with the context, or application environment, as is know in ZF, (e.g., production, development, testing , etc.) in which the application is being run.
log.partitionFrequency
Specifies when new log files get created and take on any of the following values: daily, weekly, monthly and yearly.

Next, in the Bootstrap.php, add _initLog()function as show below to dynamically create the log files based on the customized settings we provided in the application.ini.
The function simply alters the file name for the Stream logger by appending a unique string to the selected base file name based on the log.partitionFrequency value.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initLog() {
        $options = $this->getOption('resources');

        $partitionConfig = $this->getOption('log');
        $logOptions = $options['log'];

        $baseFilename = $logOptions['stream']['writerParams']['stream'];
        if ($partitionConfig['partitionStrategy'] == 'context'){
            $baseFilename = $partitionConfig['path'].'/'.APPLICATION_ENV;
        }

        $logFilename = '';
        switch(strtolower($partitionConfig['partitionFrequency'])){
            case 'daily':
                $logFilename = $baseFilename.'_'.date('Y_m_d');
                break;

            case 'weekly':
                $logFilename = $baseFilename.'_'.date('Y_W');
                break;

            case 'monthly':
                $logFilename = $baseFilename.'_'.date('Y_m');
                break;

            case 'yearly':
                $logFilename = $baseFilename.'_'.date('Y');
                break;

            default:
                $logFilename = $baseFilename;

        }

        $logOptions['stream']['writerParams']['stream'] = $logFilename;

        $logger = Zend_Log::factory($logOptions);
        Zend_Registry::set('logger', $logger);

        return $logger;
    }
}
About these ads

1 Comment »

  1. Thanks for that awesome posting. It saved MUCH time :-)

    Comment by replacement social security card — April 3, 2012 @ 5:31 am


RSS feed for comments on this post. TrackBack URI

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

The WordPress Classic Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: