2

我的 symfony2 日志中出现了很多 NotFoundHttpException 和 AccessDeniedExceptions。我怎样才能将这些移动到另一个文件(而不是与我机器上的所有其他日志混合)。

4

2 回答 2

2

首先,像这样创建一个异常事件侦听器(从此处的答案借用,但修改为像正常一样转发到树枝控制器 - 从 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 回答
1

仅供参考,Symfony >= 2.2您需要使用该服务:

$e = $event->getException();
$notFoundException = '\Symfony\Component\HttpKernel\Exception\NotFoundHttpException';
if ($e instanceof $notFoundException) {
    // Log the error
    $this->pageNotFoundLogger->info($e->getMessage());

    /** @var ExceptionController $exceptionController */
    $exceptionController = $this->container->get('twig.controller.exception');
    $request = $event->getRequest();
    $response = $exceptionController->showAction($request, FlattenException::create($e), $this->logger, $request->getRequestFormat());

    $event->setResponse($response);
    $event->stopPropagation();
    return;
}

在此示例中,我使用容器来获取服务,但您最好直接在service.yml.

于 2013-09-24T05:25:51.517 回答