gpt4 book ai didi

php - 使用 Symfony2 更详细地记录错误

转载 作者:IT王子 更新时间:2023-10-28 23:57:39 24 4
gpt4 key购买 nike

我的生产日志记录使用了以下配置:

monolog:
handlers:
mail:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [streamed, buffered]
streamed:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
# buffered is used to accumulate errors and send them as batch to the email address
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: info@....com
to_email: info@....com
subject: Error Occurred!
level: debug

这会发送这样的电子邮件:

[2012-03-21 21:24:09] security.DEBUG: Read SecurityContext from the session [] []

[2012-03-21 21:24:09] security.DEBUG: Reloading user from user provider. [] []

[2012-03-21 21:24:09] security.DEBUG: Username "jakob.asdf" was reloaded from user provider. [] [] [2012-03-21 21:24:09] request.INFO: Matched route "_user_settings" (parameters: "_controller": "...Bundle\Controller\UserController::settingsAction", "username": "Jakob.asdf", "_route": "_user_settings") [] []

[2012-03-21 21:24:09] request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: ...Bundle\Entity\User object not found. (uncaught exception) at /var/www/.../vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DoctrineParamConverter.php line 50 [] []

[2012-03-21 21:24:09] security.DEBUG: Write SecurityContext in the session [] []

我真的很想在这里有一个堆栈跟踪,或者至少是我的 Controller 中触发错误的行号。否则真的是很多人猜测可能出了什么问题。

现在,问题是:有什么方法可以实现这样更详细的日志记录?

最佳答案

是的,这是可以实现的。

创建一个 ExceptionListener 类。

//namespace declarations

class ExceptionListener{
/**
* @var \Symfony\Component\HttpKernel\Log\LoggerInterface
*/
private $logger =null;

/**
* @param null|\Symfony\Component\HttpKernel\Log\LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
if($this->logger === null)
return;
$exception = $event->getException();
$flattenException = FlattenException::create($exception);
$this->logger->err('Stack trace');
foreach ($flattenException->getTrace() as $trace) {
$traceMessage = sprintf(' at %s line %s', $trace['file'], $trace['line']);
$this->logger->err($traceMessage);
}
}
}

然后注册监听器。

 kernel.listener.your_listener_name:
class: FQCN\Of\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException , priority: -1}
- { name: monolog.logger, channel: mychannel }
arguments:
- "@logger"

您可以根据需要对其进行调整。

关于php - 使用 Symfony2 更详细地记录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9819023/

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