Search code examples
phpsymfonyerror-handlingproduction

Symfony Production Mode Error Reports and POST values


I'm using Symfony 4 but I expect this is much the same for 2 and 3 too.

Production Mode error reports from the swift monolog handler return the complete request URL for GET requests, so it's fairly easy to reproduce the error in development.

But, if it's a POST request, you're in trouble as no POST values are provided.

I've had a dig around in the error handling and logging components but nothing is immediately obvious.

Has anyone else come across this problem and found a fix for it?


Solution

  • Based on a comment from @Puya Sarmidani... this is what I did in the end:

    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);
        }
    }