我的symfony2日志中有很多NotFoundHttpException和AccessDeniedExceptions。如何将这些文件移动到另一个文件(而不是与我机器上的所有其他日志混合)。
答案 0 :(得分:2)
首先,创建一个像这样的异常事件监听器(从答案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 for ya。
答案 1 :(得分: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
中传递服务。