【问题标题】:Symfony Production Mode Error Reports and POST valuesSymfony 生产模式错误报告和 POST 值
【发布时间】:2019-04-23 19:50:58
【问题描述】:

我使用的是 Symfony 4,但我希望 2 和 3 也差不多。

来自 swift monolog 处理程序的生产模式错误报告返回 GET 请求的完整请求 URL,因此在开发中重现错误相当容易。

但是,如果它是一个 POST 请求,你就会遇到麻烦,因为没有提供任何 POST 值。

我对错误处理和日志记录组件进行了深入研究,但没有什么是显而易见的。

有没有其他人遇到过这个问题并找到了解决方法?

【问题讨论】:

    标签: php symfony error-handling production


    【解决方案1】:

    基于@Puya Sarmidani的评论...这就是我最后所做的:

    config/services.yaml:

    App\Services\MonologExtraProcessor:
        tags:
            - { name: monolog.processor }
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    

    src/Services/MonologExtraProcessor.php:

    namespace App\Services;
    
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    
    class MonologExtraProcessor
    {
        private $postParams = null;
    
        public function __invoke(array $record)
        {
            if ($this->postParams !== null) {
                $record['extra']['postParams'] = $this->postParams;
            }
            return $record;
        }
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            $postParams = $event->getRequest()->request->all();
            $this->postParams = empty($postParams) ? null : serialize($postParams);
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,我对这类难以追踪的问题使用了修复程序。 您可以在 config_prod.yml/config/packages/prod/monolog.yaml 中为 symfony 4 启用 monolog swift 处理程序。这样,当出现错误时,您将收到与开发模式相同的电子邮件发生。 (取决于 action_level)。

      查看symfony 4下面的代码(首先需要安装monolog)

          monolog:
            handlers:
              main:
                type:         fingers_crossed
                # 500 errors are logged at the critical level
                action_level: critical
                # to also log 400 level errors (but not 404's):
                # action_level: error
                # excluded_404s:
                #     - ^/
                handler:      deduplicated
              deduplicated:
                type:    deduplication
                handler: swift
              swift:
                type:       swift_mailer
                from_email: '**FROM EMAIL**'
                to_email:   '**TO EMAIL**'
                # or list of recipients
                # to_email:   ['dev1@example.com', 'dev2@example.com', ...]
                subject:    'An Error Occurred! %%message%%'
                level:      debug
                formatter:  monolog.formatter.html
                content_type: text/html
      

      【讨论】:

      • 这正是我们使用的配置。也许我没有足够清楚地说明我的问题......问题是 Swift 处理程序在它的错误报告中不包含 POST 数据。
      • 啊,我明白了,也许这篇文章会对你有所帮助。我知道它适用于 symfony2,但稍作修改我也可以用于 symfony4。 riptutorial.com/symfony2/example/22708/…
      猜你喜欢
      • 2018-11-09
      • 2011-09-05
      • 2018-11-25
      • 2015-04-23
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      相关资源
      最近更新 更多