Logging in an application is important for many reasons. Often, there are problems to reach an updating file on a server, so other options for logging may be relevant such as using a database table to store log-data
In ZendFramework, there are several loggers to choose between, such as logging to file or using headers to Firebug, but in this example, I have selected to focus on the Zend_Log_Writer_Db.
The database logger will write to a database table that you created and will use a column mapping to select which columns that will be used for storing the log data. This includes the columns:
- priority
- message
- timestamp
- priorityName
Since you may need to use the logger in the application, it is a good idea to create a method in the Bootstrap.php file to initialize the logger and store it in the registry for easy retrieval in the application.
This example uses the default database defined in the application.ini with my settings as:
resources.db.adapter = "PDO_MYSQL" resources.db.params.host = "127.0.0.1" resources.db.params.port = "3306" resources.db.params.username = "xxxxxxx" resources.db.params.password = "xxxxxxx" resources.db.params.dbname = "jusote"
The database table is created as:
CREATE TABLE logger ( timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, priority` varchar(30) , priorityName` varchar(40) , message text )
My method in Bootstap.php
protected function _initLogger()
{
$columnMapping = array('priority' => 'priority', 'message' => 'message',
'timestamp' => 'timestamp', 'priorityName' => 'priorityName');
$resource = $this->getPluginResource('db');
$db = $resource->getDbAdapter();
$writer = new Zend_Log_Writer_Db($db, 'logger', $columnMapping);
$logger = new Zend_Log($writer);
Zend_Registry::set('logger', $logger);
}
The log can then later be retrieved and used as
$logger = Zend_Registry::get('logger');
$logger->log('RunTestsession.run() Starting for session id=' . $testsessionId , Zend_Log::INFO);

