Posts tagged: logging

Log to DB using Zend Framework

By , Tuesday 14th April 2009 9:06 pm

I’ve managed to get a site up and running with the Zend Framework, everything is logging nicely to FireBug/FirePHP so next step was to log to the DB. I also wanted to log some additional information using the framework such as user agent, date and time, get and post variables. So to extend the manual a little here’s what I did:

// Set up logging to DB
$db = Zend_Registry::get('dbAdapters');
$db = $db['general'];

$columnMapping = array( 'priority' => 'priority',
'message' => 'message',
'datetime' => 'timestamp',
'user_agent'=> 'user_agent',
'get_vars' => 'get_vars',
'post_vars' => 'post_vars',
'site' => 'site'

$writerDb = new Zend_Log_Writer_Db($db, 'error_logging', $columnMapping);
$logger = new Zend_Log($writerDb);

$logger->setEventItem('datetime',date('Y-m-d H:i:s'));

$logger->info('Informational message');

Where the array keys in $columnMapping are my column names. ‘Priority’ and ‘message’ are understood by Zend_Log_Writers but the additional fields were added to give me some additional information.

Obviously this assumes that you have logging working using one of the other writers first :)

Panorama Theme by Themocracy

3 visitors online now
0 guests, 3 bots, 0 members
Max visitors today: 6 at 03:27 am UTC
This month: 15 at 10-10-2017 02:55 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC