目前,我有一个单一的邮件发送给我时,有错误。我发现404个错误只是污染了我的电子邮件,我的提供商最终因为我自己发送的电子邮件数量而暂停了我的帐户。我决定排除所有404错误,因为所有这些错误都是由于机器人在寻找漏洞,而不是来自客户端。
排除代码:
excluded_404s: - ^/
我现在看到的问题是,如果机器人使用的是GET以外的http方法,symfony仍然记录404错误。我的电子邮件现在被这样的条目污染了
HEAD :80/phpmyAdmin/
如何排除所有404错误,包括使用除GET以外的http方法的错误?
编辑:哦,天哪。初学者弄错了。在最后一次部署配置之后,我似乎没有清除prod缓存,并且正在计算配置是缓存的。我正在使用部署程序来部署我的代码更新,但我想它缺少清晰的缓存命令。
发布于 2019-08-15 13:11:54
Symfony 4.1.可以将upword配置为忽略HTTP代码:
config/packages/monolog.yaml
monolog:
handlers:
main:
# ...
type: 'fingers_crossed'
excluded_http_codes: [404]https://symfony.com/blog/new-in-symfony-4-1-ignore-specific-http-codes-from-logs
发布于 2019-10-30 09:31:55
我知道这是一个古老的问题,但这是第一个谷歌搜索。同时,我为symfony 3.4找到了一个解决方案,如果您需要从日志中排除所有404错误,您可以创建一个ExceptionListener并检查异常是否为NotFoundHttpException。如果是这样的话,那么只返回一个响应,这样事件就会停止,记录器就不会处理异常。
src/AppBundle/EventListener/ExceptionListener.php
namespace AppBundle\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (! $exception instanceof NotFoundHttpException) {
return;
}
$response = new Response($exception->getMessage(), $exception->getStatusCode());
// returning a response stop the event propagation
$event->setResponse($response);
}
}src/AppBundle/Resources/config/services.yml
services:
app.exception.listener:
class: AppBundle\EventListener\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception }发布于 2020-06-13 16:29:25
在Symfony 4/5中,您可以:
<?php
declare(strict_types=1);
namespace App\EventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExceptionListener
{
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if ($exception instanceof NotFoundHttpException) {
$response = new Response($exception->getMessage(), $exception->getStatusCode());
$event->setResponse($response);
}
return;
}
}和
App\EventListener\ExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception }https://stackoverflow.com/questions/42327523
复制相似问题