Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to log errors in Zend Framework 1?

We built an app in Zend Framework (v1) and have not worked a lot in setting up error reporting and logging. Is there any way we could get some level or error reporting without too much change in the code? Is there a ErrorHandler plugin available?

The basic requirement is to log errors that happens within the controller, missing controllers, malformed URLs, etc.

I also want to be able to log errors within my controllers. Will using error controller here, help me identify and log errors within my controllers? How best to do this with minimal changes?

like image 803
Pasta Avatar asked Mar 15 '10 22:03

Pasta


People also ask

Is Zend Framework Good?

When it comes to PHP frameworks, Zend is counted among the best. Zend Framework offers lots of benefits for creating feature-rich and dynamic web solutions. MVC features and a strong component library have made Zend a popular PHP framework for creating a myriad of web solutions.

What is controller in Zend Framework?

In Zend Framework 2, the controller is a class that is generally called {Controller name}Controller. Note that {Controller name} must start with a capital letter. This class lives in a file called {Controller name}Controller. php within the Controller directory for the module.

How do I log an exception in Zend Server?

For instance, if you wish to log an exception, you can log not just the exception message, but pass the entire exception object to the function, and then inspect the object within the Zend Server event monitor. In order to use this log writer, Zend Monitor must be both installed and enabled.

What is a writer in Zend logs?

A Writer is an object that inherits from Zend_Log_Writer_Abstract . A Writer's responsibility is to record log data to a storage backend. Zend_Log_Writer_Stream sends log data to a » PHP stream . To write log data to the PHP output buffer, use the URL php://output.

What is Zend_log_writer_zendmonitor?

Zend_Log_Writer_ZendMonitor allows you to log events via Zend Server's Monitor API. This allows you to aggregate log messages for your entire application environment in a single location. Internally, it simply uses the monitor_custom_event () function from the Zend Monitor API .

What happens if Zend_log_writer_mail receives errors from the script?

For example, if the subject prepend text reads "Errors from script", the subject of an email generated by Zend_Log_Writer_Mail with two warnings and five errors would be "Errors from script (warn = 2; error = 5)". If subject prepend text is not in use via Zend_Log_Writer_Mail, the Zend_Mail subject line, if any, is used.


1 Answers

I would use Zend_Log and use the following strategy.

If you are using Zend_Application in your app, there is a resource for logging. You can read more about the resource here

My advice would be to choose between writing to a db or log file stream. Write your log to a db if you plan on having some sort of web interface to it, if not a flat file will do just fine.

You can setup the logging to a file with this simple example

  resources.log.stream.writerName = "Stream"
  resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
  resources.log.stream.writerParams.mode = "a"
  resources.log.stream.filterName = "Priority"
  resources.log.stream.filterParams.priority = 4

Also, I would suggest sending Critical errors to an email account that is checked regularly by your development team. The company I work for sends them to [email protected] and that forwards to all of the developers from production sites.

From what I understand, you can't setup a Mail writer via a factory, so the resource won't do you any good, but you can probably set it up in your ErrorController or Bootstrap.

  $mail = new Zend_Mail();

  $mail->setFrom('[email protected]')
       ->addTo('[email protected]');
  $writer = new Zend_Log_Writer_Mail($mail);
  // Set subject text for use; summary of number of errors is appended to the
  // subject line before sending the message.
  $writer->setSubjectPrependText('Errors with script foo.php');

  // Only email warning level entries and higher.
  $writer->addFilter(Zend_Log::WARN);
  $log = new Zend_Log();
  $log->addWriter($writer);

  // Something bad happened!

  $log->error('unable to connect to database');

  // On writer shutdown, Zend_Mail::send() is triggered to send an email with
  // all log entries at or above the Zend_Log filter level.

You will need to do a little work to the above example but the optimal solution would be to grab the log resource in your bootstrap file, and add the email writer to it, instead of creating a second log instance.

like image 132
Travis Avatar answered Sep 28 '22 13:09

Travis