Logging information in database using Zend Log

31 Oct

Recently I came across a new requirement.
“Store user specific information in the database using Zend Log component”.
Although Zend has done tremendous job by creating lots of components, however documentation doesn’t have appropriate examples, so beginner and novice face lot of difficulties in using those components in the beginning.
Although I’d used Zend Log component before-for storing log information in text file, however I hadn’t used it for storing log information in the database.
Studying online manual doesn’t help me lot. Although I got basic idea from the manual, however I was unable to map information to my table- table fields were already defined.
After googling for about some time I got the solution, but example hadn’t cover how to map log information to database, instead explained how to map log data to the xml file.
However I got help from that article and wrote my own code.
I think it’ll help you out.

$columnMapping = array(
‘table_column_1 =>’map1’,
‘ table_column_1’=>’map2’,
‘ table_column_1’=>’map3’,
‘ table_column_1’ => ‘map4’

$writer = new Zend_Log_Writer_Db($db, ‘log_actions’, $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem(‘ map1’, ‘ column 1 info’);
$logger->setEventItem(map1, ‘ column 2 info’);
$logger->setEventItem(‘ map1’, ‘ column 3 info’);
$logger->setEventItem(map4′, ‘ column 4 info’);

First we get instance to our $db. I assume that you have made necessary configuration in your boot file and story reference to “db” using Zend_Registry.
Next we define our “columnMapping” array. Here we are mapping our database table columns (table_column_1, table_column_2…..) to the mapping variables- these variables are used in setEventItem() method later in our code.
In the next line we have defined our writer. Zend has different writers for Db, Xml and file-streams.
As we are logging information in database so we are using Zend_Log_Writer_Db. Give database adapter, table name and columnMapping array as arguments.
Now create Zend_Log instance and give it writer instance as parameter.
The final thing is to store information in the database through mapping fields using setEventItem method.
That it.


2 Responses to “Logging information in database using Zend Log”

  1. Paolo January 21, 2009 at 12:35 pm #

    Hi Faheem,

    thank you very much for your useful example and explanation!

    Kind regards,


    I agree with you about Zend documentation

  2. zorex November 7, 2010 at 4:27 pm #

    Thanks a lot, i have been looking for this.
    Either i’m lazy or there is a real lack documentation on this chapter,at one point i was thinking to drop zend db loggin and wrote my own ! You saved me.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: