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->setEventItem('user_agent',$_SERVER['HTTP_USER_AGENT']);
$logger->setEventItem('get_vars',print_r($_GET,true));
$logger->setEventItem('post_vars',print_r($_POST,true));
$logger->setEventItem('site',SITE);

$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 :)

Liked this post? Follow this blog to get more. 

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

2 visitors online now
1 guests, 1 bots, 0 members
Max visitors today: 5 at 06:43 am UTC
This month: 14 at 11-05-2017 07:08 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC