Monday, October 27, 2008

How to use Zend_Log

Setup the logger in the Bootstrap file:
  1.    
  2. //initiate the logger - Log to a file  
  3. $writer = new Zend_Log_Writer_Stream($root.'\log\test.txt');  
  4. $logger = new Zend_Log($writer);  
  5. //Log to Database.  
  6. $columnMap = array('lvl' => 'priority''msg' => 'message''timestamp' => 'timestamp''lvlName' => 'priorityName');  
  7. $dbwriter = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMap);  
  8. $logger->addWriter($dbwriter);  
  9. //Log to firebug  
  10. $writerFirebug = new Zend_Log_Writer_Firebug();  
  11. $writerFirebug->setPriorityStyle(6'TRACE');  
  12. $logger->addWriter($writerFirebug);  
  13. $registry->logger = $logger;  

Using the logger:
  1.    
  2. $logger = Zend_Registry::get('logger');  


Log an exception:
  1.    
  2. $exception = new Zend_Exception('Test exception');  
  3. $logger->err($exception);  


Log a message:
  1.    
  2. $logger->info('Show Information about something');  

1 comment:

steenvinkel said...

For filtering add following to the bootstrap file:
$filter = new Zend_Log_Filter_Priority(Zend_Log::INFO);
$logger->addFilter($filter);

This will make sure no debug messages are logged.