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

前端 未结 2 1340
走了就别回头了
走了就别回头了 2021-02-04 04:46

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 t

相关标签:
2条回答
  • 2021-02-04 05:37

    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.

    0 讨论(0)
  • 2021-02-04 05:39

    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 讨论(0)
提交回复
热议问题