2

symfony2 のログに NotFoundHttpException と AccessDeniedExceptions が大量に記録されています。これらを別のファイルに移動するにはどうすればよいですか (マシン上の他のすべてのログと混ざりません)。

4

2 に答える 2

2

まず、次のように例外イベント リスナーを作成します ( answer hereから借用しましたが、通常のように twig コントローラーに転送するように変更しました - symfony の ExceptionListener からそのコードを取得しました):

class AnnoyingExceptionListener {
    private $logger;

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

    public function onKernelException(GetResponseForExceptionEvent $event) {
        static $handling;

        if (true === $handling) {
            return false;
        }

        $handling = true;

        $exception = $event->getException();
        $request = $event->getRequest();
        $type = get_class($exception);

        if(!$event) {
            $this->logger->err("Unknown kernel.exception in ".__CLASS__);
            return;
        }
        $notFoundException = '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException';
        $accessDeniedException = '\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException';

        if ($exception instanceof $notFoundException) {
            $this->logger->info($exception->getMessage());
        }
        else if ($exception instanceof $accessDeniedException) {
            $this->logger->info($exception->getMessage());
        }
        else {
            $this->logger->err("kernel.exception of type $type. Message: '". $exception->getMessage()."'\nFile: ". $exception->getFile().", line ". $exception->getLine()."\nTrace: ". $exception->getTraceAsString());
        }

        $attributes = array(
            '_controller' => 'Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction',
            'exception'   => FlattenException::create($exception),
            'logger'      => $this->logger,
            'format'      => $request->getRequestFormat(),
        );

        $request = $request->duplicate(null, null, $attributes);
        $request->setMethod('GET');

        try {
            $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
        } catch (\Exception $e) {
            $message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage());
            if (null !== $this->logger) {
                if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
                    $this->logger->crit($message);
                } else {
                    $this->logger->err($message);
                }
            } else {
                error_log($message);
            }

            // set handling to false otherwise it wont be able to handle further more
            $handling = false;

            // re-throw the exception from within HttpKernel as this is a catch-all
            return;
        }

        $event->setResponse($response);

        $handling = false;
    }

次に、次の service.yml エントリを作成します。

 failedRequestStreamHandler:
    class: Monolog\Handler\StreamHandler
    arguments:
      - %kernel.logs_dir%/failed_requests.log
      - debug

 failedRequestLogger:
    class: Symfony\Bridge\Monolog\Logger
    arguments: ['failedRequests']
    calls: [[ pushHandler, [@failedRequestStreamHandler] ]]

 kernel.listener.annoying_exception_listener:
        class: TMD\SharedBundle\Listener\AnnoyingExceptionListener
        arguments: [@failedRequestLogger]
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

少し肥大化していますが、それが Symfony2 です。

于 2013-02-28T21:45:16.353 に答える