Today I stuck with an interesting problem.
I wanted to log different activities using Zend DB Logger.
Zend Framework provides a nice way to log with some default parameters i.e.
- timestamp
- message
- priority
- priorityName
Syntax for logging is as under :-
$columnMapping = array( 'log_message' => 'message', 'created_at' => 'timestamp');
$writer = new Zend_Log_Writer_Db(Zend_Db_Table::getDefaultAdapter(), 'watchdog', $columnMapping);
$logger = new Zend_Log($writer);
$logger->info('This is log message');
So if watchdog table contains two fields i.e. log_message and created_at , then after execution of above code, watchdog table will contain a record with 'This is a log message' and timestamp value of execution in respective fields.
But what if we want to log other data such as user name etc.
Then how to map those fields with fields in database table?
This can be achieved as following :-
$columnMapping = array('log_obj_id' => 'log_obj_id',
'type' => 'type',
'message' => 'message',
'created_at' => 'timestamp',
'data' => 'data'
);
$writer = new Zend_Log_Writer_Db(Zend_Db_Table::getDefaultAdapter(), 'watchdog', $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem('log_obj_id', $log_params['log_obj_id']);
$logger->setEventItem('type', $log_params['type']);
$logger->setEventItem('data', $log_params['data']);
$logger->info($log_params['message']);
So we can see that log_obj_id, type and data fields are binded to customized values whereas timestamp and message fields are using ZF's built in values.
Hopefully it will be useful for others.
Cheers!