将 symfony2 NotFoundHttpException 移动到不同的文件 [英] move symfony2 NotFoundHttpException to different file

查看:46
本文介绍了将 symfony2 NotFoundHttpException 移动到不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 symfony2 日志中收到很多 NotFoundHttpException 和 AccessDeniedExceptions.如何将这些文件移动到另一个文件(而不是与我机器上的所有其他日志混合).

I'm getting a lot of NotFoundHttpException and AccessDeniedExceptions in my symfony2 logs. How can I move these to another file (and not intermingled with all of the other logs on my machine).

推荐答案

首先,像这样创建一个异常事件监听器(借用回答 此处,但修改为像往常一样转发到树枝控制器 - 从 symfony 的 ExceptionListener 获得该代码):

First, create an exception event listener like so (borrowed from answer here, but modified to forward to twig controller like normal - got that code from symfony's 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 条目:

Then create the following service.yml entries:

 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 给你的.

A bit bloated, but that's Symfony2 for ya.

这篇关于将 symfony2 NotFoundHttpException 移动到不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆