简体   繁体   English

PHP-FPM将堆栈跟踪日志分解为单独的事件

[英]PHP-FPM breaks up stack trace log into separate events

I have a problem with PHP-FPM registering a single event as multiple events. 我有一个问题,PHP-FPM将单个事件注册为多个事件。 Take for example the stack trace below: 以下面的堆栈跟踪为例:

[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "NOTICE: PHP message: PHP Fatal error:  Uncaught exception 'Zend_View_Exception' with message 'script 'new-layout.mobile.phtml' not found...."
[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "Stack trace:"
[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "#0 /usr/share/nginx/html/site.com/142-webapp/library/Zend/View/Abstract.php(884): Zend_View_Abstract->_script('new-layout.mobi...')"
[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "#1 /usr/share/nginx/html/site.com/142-webapp/library/Zend/Layout.php(796): Zend_View_Abstract->render('new-layout.mobi...')"
[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "#2 /usr/share/nginx/html/site.com/142-webapp/library/Zend/Layout/Controller/Plugin/Layout.php(143): Zend_Layout->render()"
[30-Jul-2014 05:38:50] WARNING: [pool www] child 11606 said into stderr: "#3 /usr/share/nginx/html/site.com/142-webapp/library/Zend/Controller/Plugin/Broker...."

As you can see, each line of the stack trace is effectively a separate event with its own timestamp. 如您所见,堆栈跟踪的每一行实际上都是一个具有自己时间戳的单独事件。 This is problematic when forwarding logs to another service for analysis because then each stack trace will be broken up when it should be considered as one event. 将日志转发到另一个服务进行分析时会出现问题,因为当应将每个堆栈跟踪视为一个事件时,它们将被分解。 At the moment I am using Kibana 3 and it is a nightmare viewing and managing stack traces since each line is a separate event and the individual events are not always in chronological order. 目前我正在使用Kibana 3,这是一个查看和管理堆栈跟踪的噩梦,因为每一行都是一个单独的事件,并且各个事件并不总是按时间顺序排列。

How do I make php-fpm register each stack trace as one event ? 如何让php-fpm将每个堆栈跟踪注册为一个事件?

Unfortunately not 不幸的是

PHP-FPM simply logs each line of PHP output as a separate event. PHP-FPM只是将每行PHP输出记录为一个单独的事件。 There's nothing you can do in/with PHP-FPM to change this. 在PHP-FPM中你无法做任何事情来改变它。

PHP Code PHP代码

You'll need to "fix" this in your application (PHP code). 您需要在应用程序中修复此问题(PHP代码)。 There are 3 ways you can influence the way PHP reports errors, and you'll probably want to use all 3: 有三种方法可以影响PHP报告错误的方式,你可能想要使用全部3:

  • Register a custom error handler with set_error_handler() . 使用set_error_handler()注册自定义错误处理程序。 This handler is called on all errors except E_ERROR , E_PARSE , E_CORE_ERROR , E_CORE_WARNING , E_COMPILE_ERROR , E_COMPILE_WARNING , and most of E_STRICT raised in the file where set_error_handler() is called. 除了E_ERRORE_PARSEE_CORE_ERRORE_CORE_WARNINGE_COMPILE_ERRORE_COMPILE_WARNING以及调用set_error_handler()的文件中引发的大部分E_STRICT之外,将调用此处理程序。

  • Register a custom exception handler with set_exception_handler() . 使用set_exception_handler()注册自定义异常处理程序。 This handler is called when an uncaught exception occurs. 发生未捕获的异常时会调用此处理程序。

  • Register a custom shutdown function with register_shutdown_function() . 使用register_shutdown_function()注册自定义关闭功能。 This function is called after script execution finishes or exit() is called. 在脚本执行完成或调用exit()之后调用此函数。 This one is useful for detecting errors that are not handled with the error handler. 这个用于检测错误处理程序未处理的错误。

Log library 日志库

I can advise you to have a look at Monolog . 我可以建议你看一下Monolog It's a PSR-3 complaint logging library which also facilitates what I described above. 这是一个PSR-3投诉记录库,它也促进了我上面描述的内容。

In addition it has an impressive list of "handlers" which can write the logs to all sorts of services. 此外,它还有一个令人印象深刻的“处理程序”列表,可以将日志写入各种服务。 Chances are the service you're using now is among them! 您现在使用的服务很可能就在其中!

Alternative 替代

Another option is to create a proxy script that will read the PHP-FPM log files and buffers lines until a "full event" is gathered. 另一种选择是创建一个代理脚本,该脚本将读取PHP-FPM日志文件并缓冲行,直到收集到“完整事件”。 Then writing that as a single entry to the service you're using. 然后将其作为单个条目写入您正在使用的服务。

I would advise you not to go this way. 我建议你不要这样走。 Writing such a script can be tricky and is very error-prone. 编写这样的脚本可能很棘手并且非常容易出错。 Logging from your application itself is much more stable and reliable. 从您的应用程序本身进行日志记录更加稳定可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM