开发者

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

开发者 https://www.devze.com 2022-12-23 04:29 出处:网络
开发者_运维百科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

开发者_运维百科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?


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 errors@companyname.com 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('errors@example.org')
       ->addTo('project_developers@example.org');
  $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.


You can use Zend_Controller_Plugin_ErrorHandler . As you can see on the documentation page there is an example that checks for missing controller/action and shows you how to set the appropriate headers.

You can then use Zend_Log to log your error messages to disk/db/mail.

0

精彩评论

暂无评论...
验证码 换一张
取 消