MonologBu​​ndle FingerCrossedHandler:如何配置excluded_404s

时间:2016-05-18 12:13:34

标签: symfony monolog

我正在使用Symfony 2.8.6应用程序,我尝试按照herehere所述配置我的Monolog。 这是我的Monolog配置(包版本2.11.1):

monolog:
    handlers:
        main_critical:
            type:           fingers_crossed
            action_level:   error
            handler:        grouped
            channels:       ["!doctrine", "!event", "!php"]
            excluded_404s:
                - ^/
        grouped:
            type:           group
            members:        [streamed, crash, buffered]
        streamed:
            type:           rotating_file
            max_files:      10
            path:           "%kernel.logs_dir%/%kernel.environment%.log"
            level:          notice
        crash:
            type:           rotating_file
            max_files:      10
            path:           "%kernel.logs_dir%/%kernel.environment%.critical.log"
            level:          critical
        buffered:
            type:           buffer
            handler:        swift
        swift:
            type:           swift_mailer
            from_email:     noreply@xxxxxxx.com
            to_email:       user@xxxxx.com
            subject:        "[App] - Errore %kernel.environment%"
            level:          error

Monolog记录每404错误,也丢失了css和js等资产。 怎么能避免这个? 我哪里错了?

问题可能与我的ExceptionListener有关吗?

     /**
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {   
        //The logger object is my logger
        $this->logger->log($event->getException(), $event->getRequest());
        if (!$event->getRequest()->isXmlHttpRequest()) {
            return;
        }
        $event->setResponse(new JsonResponse('message' => 'Ops. Houston we have a problem.'));
    }

谢谢。

2 个答案:

答案 0 :(得分:0)

您可以在onKernelException方法中过滤错误。见下面的例子:

<?php

namespace AppBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class AppExceptionListener
{
    private $logger;

    public function __construct($logger)
    {
        $this->logger = $logger;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if(!$exception instanceof NotFoundHttpException)
        {
            $this->logger->info('Error');
        }
    }

}

答案 1 :(得分:0)

我发布了我的解决方法。问题与Monolog无关,但与我的记录器服务有关。

因为在我调用

之前没有通过异常之前,我已经包装了如何记录异常
$logger->error('message error');

因此,当Monolog尝试启用NotFoundActivationStrategy(文件位于/vendor/symfony/monolog-bundle/NotFoundActivationStrategy.php)时,此检查失败:

public function isHandlerActivated(array $record)
{
    $isActivated = parent::isHandlerActivated($record);
    if (
        $isActivated
        && $this->request
        && isset($record['context']['exception'])
        && $record['context']['exception'] instanceof HttpException
        && $record['context']['exception']->getStatusCode() == 404
     ) {
        //blacklist is the pattern that you can set on config.yml at excluded_404s node
        return !preg_match($this->blacklist, $this->request->getPathInfo());
     }

     return $isActivated;
 }

所以我决定用

$this->logger->error('message', array('exception' => $exception));

希望这有助于某人。