MonologBundle FingerCrossedHandler:如何配置 excluded_404s
MonologBundle FingerCrossedHandler: how to configure excluded_404s
我正在开发 Symfony 2.8.6 应用程序,我尝试按照 here and here 所述配置我的 Monolog。
这是我的 Monolog 配置(捆绑版本 2.11.1):
monolog:
handlers:
main_critical:
type: fingers_crossed
action_level: error
handler: grouped
channels: ["!doctrine", "!event", "!php"]
excluded_404s:
- ^/
grouped:
type: group
members: [streamed, crash, buffered]
streamed:
type: rotating_file
max_files: 10
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: notice
crash:
type: rotating_file
max_files: 10
path: "%kernel.logs_dir%/%kernel.environment%.critical.log"
level: critical
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: noreply@xxxxxxx.com
to_email: user@xxxxx.com
subject: "[App] - Errore %kernel.environment%"
level: error
Monolog 记录每个 404 错误,还缺少 css 和 js 等资产。
如何避免这种情况?
我哪里错了?
也许问题与我的 ExceptionListener 有关?
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
//The logger object is my logger
$this->logger->log($event->getException(), $event->getRequest());
if (!$event->getRequest()->isXmlHttpRequest()) {
return;
}
$event->setResponse(new JsonResponse('message' => 'Ops. Houston we have a problem.'));
}
谢谢。
您可以在 onKernelException 方法中过滤错误。请参见下面的示例:
<?php
namespace AppBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AppExceptionListener
{
private $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if(!$exception instanceof NotFoundHttpException)
{
$this->logger->info('Error');
}
}
}
我post我是怎么解决的。这个问题与 Monolog 无关,而是与我的记录器服务有关。
因为我包装了如何记录异常,在我调用时没有传递异常之前
$logger->error('message error');
因此,当 Monolog 尝试启用 NotFoundActivationStrategy(文件位于 /vendor/symfony/monolog-bundle/NotFoundActivationStrategy.php
)时,此检查失败:
public function isHandlerActivated(array $record)
{
$isActivated = parent::isHandlerActivated($record);
if (
$isActivated
&& $this->request
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
&& $record['context']['exception']->getStatusCode() == 404
) {
//blacklist is the pattern that you can set on config.yml at excluded_404s node
return !preg_match($this->blacklist, $this->request->getPathInfo());
}
return $isActivated;
}
所以我解决了
$this->logger->error('message', array('exception' => $exception));
希望这对某人有所帮助。
我正在开发 Symfony 2.8.6 应用程序,我尝试按照 here and here 所述配置我的 Monolog。 这是我的 Monolog 配置(捆绑版本 2.11.1):
monolog:
handlers:
main_critical:
type: fingers_crossed
action_level: error
handler: grouped
channels: ["!doctrine", "!event", "!php"]
excluded_404s:
- ^/
grouped:
type: group
members: [streamed, crash, buffered]
streamed:
type: rotating_file
max_files: 10
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: notice
crash:
type: rotating_file
max_files: 10
path: "%kernel.logs_dir%/%kernel.environment%.critical.log"
level: critical
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: noreply@xxxxxxx.com
to_email: user@xxxxx.com
subject: "[App] - Errore %kernel.environment%"
level: error
Monolog 记录每个 404 错误,还缺少 css 和 js 等资产。 如何避免这种情况? 我哪里错了?
也许问题与我的 ExceptionListener 有关?
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
//The logger object is my logger
$this->logger->log($event->getException(), $event->getRequest());
if (!$event->getRequest()->isXmlHttpRequest()) {
return;
}
$event->setResponse(new JsonResponse('message' => 'Ops. Houston we have a problem.'));
}
谢谢。
您可以在 onKernelException 方法中过滤错误。请参见下面的示例:
<?php
namespace AppBundle\Listener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AppExceptionListener
{
private $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if(!$exception instanceof NotFoundHttpException)
{
$this->logger->info('Error');
}
}
}
我post我是怎么解决的。这个问题与 Monolog 无关,而是与我的记录器服务有关。
因为我包装了如何记录异常,在我调用时没有传递异常之前
$logger->error('message error');
因此,当 Monolog 尝试启用 NotFoundActivationStrategy(文件位于 /vendor/symfony/monolog-bundle/NotFoundActivationStrategy.php
)时,此检查失败:
public function isHandlerActivated(array $record)
{
$isActivated = parent::isHandlerActivated($record);
if (
$isActivated
&& $this->request
&& isset($record['context']['exception'])
&& $record['context']['exception'] instanceof HttpException
&& $record['context']['exception']->getStatusCode() == 404
) {
//blacklist is the pattern that you can set on config.yml at excluded_404s node
return !preg_match($this->blacklist, $this->request->getPathInfo());
}
return $isActivated;
}
所以我解决了
$this->logger->error('message', array('exception' => $exception));
希望这对某人有所帮助。