在Zend Framework 1中记录错误的最佳方法是什么? [英] What is the best way to log errors in Zend Framework 1?

查看:41
本文介绍了在Zend Framework 1中记录错误的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Zend Framework(v1)中构建了一个应用程序,但在设置错误报告和日志记录方面工作不多。有什么办法可以在不对代码进行太多更改的情况下获得一些级别或错误报告?是否有可用的ErrorHandler插件?



基本要求是记录控制器内部发生的错误,缺少控制器,URL格式错误等。



我还希望能够在控制器中记录错误。会在此处中使用错误控制器,对我有帮助识别并记录控制器中的错误?

解决方案

我将使用 Zend_Log 并使用以下策略。



如果您使用的是您的应用程序中的Zend_Application,有用于记录的资源。您可以在此处

我的建议是在写入数据库或日志文件流之间进行选择。如果打算使用某种Web界面将日志写入db,即使不是平面文件也可以。



您可以将日志设置为这个简单示例的文件

  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 =优先级
resources.log.stream.filterParams.priority = 4

另外,我建议将严重错误发送到开发团队定期检查的电子邮件帐户。我工作的公司将其发送到errors@companyname.com,并将其从生产站点转发给所有开发人员。



据我了解,您无法设置通过工厂的邮件编写器,因此资源不会为您带来任何好处,但是您可以在ErrorController或Bootstrap中对其进行设置。

  $ mail = new Zend_Mail(); 

$ mail-> setFrom(’errors@example.org’)
-> addTo(’project_developers@example.org’);
$ writer = new Zend_Log_Writer_Mail($ mail);
//设置要使用的主题文本;发送消息之前,错误数量摘要会附加到
//主题行。
$ writer-> setSubjectPrependText('脚本foo.php错误');

//仅电子邮件警告级别条目和更高级别。
$ writer-> addFilter(Zend_Log :: WARN);
$ log = new Zend_Log();
$ log-> addWriter($ writer);

//发生了不好的事情!

$ log->错误(无法连接到数据库);

//在关闭编写器时,将触发Zend_Mail :: send()发送电子邮件,其中
//在Zend_Log过滤器级别或更高级别的所有日志条目。

您需要对上述示例做一些工作,但最佳解决方案是抢占在您的引导文件中记录日志资源,并向其中添加电子邮件编写器,而不是创建第二个日志实例。


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.

这篇关于在Zend Framework 1中记录错误的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆