在PHP中从生产站点获取错误的最佳方法是什么?

时间:2021-10-22 02:46:58

For most production sites, you want to know when there has been an error as soon as possible. My question is how best to get this information.

对于大多数生产站点,您希望尽快知道何时出现错误。我的问题是如何最好地获取此信息。

Usually, it's probably best to get the errors in an email as I'm not going to sit every day and watch error logs until there is an error--this would be impossible since I have 20 or more production sites on different servers. These errors could be anything including, unset variables, invalid data received, or query errors.

通常情况下,最好是在电子邮件中收到错误,因为我不会每天都坐着看错误日志直到出现错误 - 这是不可能的,因为我在不同的服务器上有20个或更多的生产站点。这些错误可能包括:未设置的变量,收到的无效数据或查询错误。

At the moment I have followed the example on PHPs websites, found here. As a result, it creates a text string along with an XML file that is then sent by email. I have modified this slightly to keep all of the errors until the script ends and then send an email with the XML files attached. (I have crashed a couple mail servers sending over >500 000 emails because of an error in a loop.) Most of the time this works perfectly. (I have also created an object to do all of the error handling.)

目前我已经在PHP网站上关注了这个例子。因此,它会创建一个文本字符串以及一个XML文件,然后通过电子邮件发送该文件。我稍微修改了这个以保留所有错误,直到脚本结束,然后发送附带XML文件的电子邮件。 (由于循环中的错误,我已经崩溃了一些邮件服务器发送超过50万封电子邮件。)大部分时间这都完美无缺。 (我还创建了一个对象来完成所有错误处理。)

The problem arises when there is a large amount of data for wddx_serialize_value() to processs. And then if there are multiple errors, then, it really ends up using a lot of memory, most of the time more than the script is allowed to use.

当wddx_serialize_value()有大量数据要处理时,就会出现问题。然后,如果有多个错误,那么它最终会占用大量内存,大部分时间都比脚本允许使用更多。

Because of this, I have added an addition gzcompress() to the XML file before storing it within the variable. This helps, but if the amount of data is very large, it still runs out of memory. (In a recent case it wanted to use around 2GB.)

因此,我在XML文件中添加了一个附加的gzcompress(),然后将其存储在变量中。这有帮助,但如果数据量非常大,它仍然会耗尽内存。 (在最近的一个案例中,它想要使用大约2GB。)

I'm wondering what other solutions there are to this or how you have modified this to make it work?

我想知道有什么其他解决方案或者你如何修改它以使其工作?

So a few requirements:

所以有一些要求:

  • it must be able to send more than just the error message to me and shouldn't make me login to the server to figure out what happened (so I can check when mobile and determine if it's an urgent matter)
  • 它必须能够发送不仅仅是错误消息给我,不应该让我登录服务器来弄清楚发生了什么(所以我可以检查何时移动并确定它是否是紧急事项)

  • there needs to be a limit on the number of emails sent. The best is still 1.
  • 需要限制发送的电子邮件数量。最好的还是1。

  • it needs to log to a file as per normal
  • 它需要按照正常情况登录到文件

Edit: I need other information related to the error, not just the error string. Often I find it's near to impossible to reproduce the error because it's caused by user input, which I don't know unless I get more information. I have tried my best to put in informative errors, but you never know how a user is going to use the system or what crap data they are going to put in. Therefore, I need more than just the error text/string.

编辑:我需要与错误相关的其他信息,而不仅仅是错误字符串。通常我发现重现错误几乎是不可能的,因为它是由用户输入引起的,除非我获得更多信息,否则我不知道。我尽力提供信息性错误,但你永远不知道用户将如何使用系统或他们将要放入哪些垃圾数据。因此,我需要的不仅仅是错误文本/字符串。

Edit 2: Can't log errors to the database because for all I know the database may not be there. Need something that is pretty much guaranteed to run. Also, the websites are not all on 1 server and I often don't have access to cron on the server (stupid hosting companies).

编辑2:无法将错误记录到数据库,因为据我所知,数据库可能不存在。需要一些非常有保证能够运行的东西。此外,这些网站并非全部在1台服务器上,而且我经常无法访问服务器上的cron(愚蠢的托管公司)。

4 个解决方案

#1


1  

Instead of setting a custom error handler, I let the errors go to the error log as usual. I set up a cron that runs periodically and monitors changes in the error log - if it changed, it sends me an email with the changes only. You can improve this process and parse the changes to better suit your needs - for example send you only errors above a certain level (such as E_WARNING and above).

我没有设置自定义错误处理程序,而是像往常一样将错误转到错误日志中。我设置了一个定期运行的cron并监视错误日志中的更改 - 如果更改,它会向我发送一封仅包含更改的电子邮件。您可以改进此过程并解析更改以更好地满足您的需求 - 例如,仅向您发送超过特定级别的错误(例如E_WARNING及更高版本)。

#2


1  

One approach could be proper exception management in your application, i.e. to have control over which errors get logged.

一种方法可能是在您的应用程序中进行适当的异常管理,即控制记录哪些错误。

Each raised exception would log the error details in a database.

每个引发的异常都会在数据库中记录错误详细信息。

Then, you could code a little application in order to search the error database, maybe just one for all your websites.

然后,您可以编写一个小应用程序来搜索错误数据库,也许只为您的所有网站。

That way you avoid large unreadable log files, because everything is indexed and quickly searchable. When your database gets too large, you can truncate your log tables via cron jobs.

这样就可以避免大量不可读的日志文件,因为所有内容都已编入索引并可快速搜索。当您的数据库太大时,您可以通过cron作业截断日志表。

#3


0  

Anacron, a cron job that emails changes to the error log* and an error log file should suffice. The cron job can do all the processing required before sending the email.

Anacron,一个通过电子邮件发送更改到错误日志*和错误日志文件的cron作业就足够了。 cron作业可以在发送电子邮件之前完成所需的所有处理。

#4


0  

One thing I have used in the past is epylog, it is a very flexible log monitoring app written in python. You can set it up to monitor your error logs and include the errors (or parts of them) in a log summary that is emailed to you.

我过去使用的一件事是epylog,它是一个用python编写的非常灵活的日志监控应用程序。您可以将其设置为监视错误日志,并将错误(或部分错误)包含在通过电子邮件发送给您的日志摘要中。

I'd lean towards storing the more detailed error data in a flat file on the server and sending you an email to tell you to check the log. A cron job that watches the error directory or files for changes and has a rate limit set would be a good way to minimize impact on your running application.

我倾向于将更详细的错误数据存储在服务器上的平面文件中,并向您发送电子邮件,告诉您检查日志。监视错误目录或文件以进行更改并具有速率限制的cron作业将是最小化对正在运行的应用程序的影响的好方法。

#1


1  

Instead of setting a custom error handler, I let the errors go to the error log as usual. I set up a cron that runs periodically and monitors changes in the error log - if it changed, it sends me an email with the changes only. You can improve this process and parse the changes to better suit your needs - for example send you only errors above a certain level (such as E_WARNING and above).

我没有设置自定义错误处理程序,而是像往常一样将错误转到错误日志中。我设置了一个定期运行的cron并监视错误日志中的更改 - 如果更改,它会向我发送一封仅包含更改的电子邮件。您可以改进此过程并解析更改以更好地满足您的需求 - 例如,仅向您发送超过特定级别的错误(例如E_WARNING及更高版本)。

#2


1  

One approach could be proper exception management in your application, i.e. to have control over which errors get logged.

一种方法可能是在您的应用程序中进行适当的异常管理,即控制记录哪些错误。

Each raised exception would log the error details in a database.

每个引发的异常都会在数据库中记录错误详细信息。

Then, you could code a little application in order to search the error database, maybe just one for all your websites.

然后,您可以编写一个小应用程序来搜索错误数据库,也许只为您的所有网站。

That way you avoid large unreadable log files, because everything is indexed and quickly searchable. When your database gets too large, you can truncate your log tables via cron jobs.

这样就可以避免大量不可读的日志文件,因为所有内容都已编入索引并可快速搜索。当您的数据库太大时,您可以通过cron作业截断日志表。

#3


0  

Anacron, a cron job that emails changes to the error log* and an error log file should suffice. The cron job can do all the processing required before sending the email.

Anacron,一个通过电子邮件发送更改到错误日志*和错误日志文件的cron作业就足够了。 cron作业可以在发送电子邮件之前完成所需的所有处理。

#4


0  

One thing I have used in the past is epylog, it is a very flexible log monitoring app written in python. You can set it up to monitor your error logs and include the errors (or parts of them) in a log summary that is emailed to you.

我过去使用的一件事是epylog,它是一个用python编写的非常灵活的日志监控应用程序。您可以将其设置为监视错误日志,并将错误(或部分错误)包含在通过电子邮件发送给您的日志摘要中。

I'd lean towards storing the more detailed error data in a flat file on the server and sending you an email to tell you to check the log. A cron job that watches the error directory or files for changes and has a rate limit set would be a good way to minimize impact on your running application.

我倾向于将更详细的错误数据存储在服务器上的平面文件中,并向您发送电子邮件,告诉您检查日志。监视错误目录或文件以进行更改并具有速率限制的cron作业将是最小化对正在运行的应用程序的影响的好方法。