Priti's Technical Corner

December 16, 2012

How to use ‘Formatters’ in Zend_log


Pre-requiste : Zend_Log Understanding

Usually, Zend_log will log the messages in below format.

‘%timestamp% %priorityName% (%priority%): %message%’;

Example:

“2012-09-18T14:39:21+05:30 (INFO) 6 : Error Cacthed”

My requirement was to have SQL logs which will ONLY Log SQL statement Something like,

INSERT INTO Test VALUES (2,’Jhon’);
INSERT INTO Test VALUES (3,’Katy’);
Update Test SET name=’Jon’ where Id=2;

1. Define a format as per your requirement
$format = ‘%message%’. PHP_EOL;

2. Instantiate Zend_Log_Formatter_Simple class and pass the user defined format
$formatter = new Zend_Log_Formatter_Simple($format);

3. Create Writer
$writerArr=array(‘stream’=>$filePath,’mode’=>’a’);
$writer=Zend_Log_Writer_Stream::factory($writerArr);
$log->addWriter($writer);

4. Apply the formatter to writer
$formatter->format(array(‘message’=>$sql));
$writer->setFormatter($formatter);

5. Write sql in the file
$log->log($sql, 7);

Where in $Log=new Zend_log(); and $filePath is path to the log file.

Simple enough :).

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: