在尝试将 Monolog 电子邮件错误与自定义异常处理程序结合起来时,我得到以下信息:
[Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException] 检测到服务“路由器”的循环引用,路径:“路由器 -> monolog.logger.router -> monolog.handler.grouped -> mana.exception.listener -> 模板 ->树枝-> 模板化.helper.logout_url”。
正如所有错误所证明的那样,我没有声称真正知道我在这里做什么。
服务摘录:
mana.exception.listener:
class: Mana\ClientBundle\EventListener\ExceptionListener
arguments: [@templating, @kernel]
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
配置摘录:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
handler: grouped
grouped:
type: group
members: [mail, custom]
mail:
type: fingers_crossed
action_level: error
handler: buffered
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: error@projectmana.org
to_email: truckeetrout@yahoo.com
subject: An Error Occurred!
level: debug
custom:
type: service
id: mana.exception.listener
自定义处理程序:
class ExceptionListener {
protected $templating;
protected $kernel;
public function __construct(EngineInterface $templating, $kernel) {
$this->templating = $templating;
$this->kernel = $kernel;
}
public function onKernelException(GetResponseForExceptionEvent $event) {
// provide the better way to display a enhanced error page only in prod environment, if you want
if ('prod' == $this->kernel->getEnvironment()) {
// exception object
$exception = $event->getException();
// new Response object
$response = new Response();
// set response content
$response->setContent(
// create you custom template AcmeFooBundle:Exception:exception.html.twig
$this->templating->render(
'ManaClientBundle:Exception:exception.html.twig', array('exception' => $exception)
)
);
// HttpExceptionInterface is a special type of exception
// that holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(500);
}
// set the new $response object to the $event
$event->setResponse($response);
}
}