gpt4 book ai didi

php - 将 symfony2 NotFoundHttpException 移动到不同的文件

转载 作者:行者123 更新时间:2023-12-04 05:08:58 24 4
gpt4 key购买 nike

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

最佳答案

首先,像这样创建一个异常事件监听器(从答案 here 借来,但修改为像正常一样转发到 Twig Controller - 从 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 给你的。

关于php - 将 symfony2 NotFoundHttpException 移动到不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com