我已将以下配置用于生产日志记录:
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:从中读取SecurityContext 会话[] []
[2012-03-21 21:24:09] security.DEBUG:从用户重新加载用户 供应商。 [] []
[2012-03-21 21:24:09] security.DEBUG:用户名“jakob.asdf”原为 从用户提供商重新加载。 [] [] [2012-03-21 21:24:09] request.INFO:匹配的路线“_user_settings”(参数: “_controller”:“... Bundle \ Controller \ UserController :: settingsAction”, “username”:“Jakob.asdf”,“_ lute”:“_ user_settings”)[] []
[2012-03-21 21:24:09] request.ERROR: Symfony的\分量\ HttpKernel \异常\ NotFoundHttpException: ...找不到Bundle \ Entity \ User对象。 (未被捕的例外)在 /var/www/.../vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DoctrineParamConverter.php 第50行[] []
[2012-03-21 21:24:09] security.DEBUG:写入SecurityContext 会话[] []
我真的很想在这里有一个堆栈跟踪,或者至少是我控制器中触发错误的行号。否则,它真的会猜测可能出现的问题。
现在,问题是:有没有办法实现这种更加冗长的日志记录?
答案 0 :(得分:24)
是的,这是可以实现的。
创建一个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"
您可以根据需要进行调整。
答案 1 :(得分:8)
我喜欢Symfony docs的解决方案。您所要做的就是将以下代码添加到services.yml
文件中:
services:
my_service:
class: Monolog\Processor\IntrospectionProcessor
tags:
- { name: monolog.processor }
这使用经过测试的处理器IntrospectionProcessor
向您的日志添加更多信息。它很可能会提取你关心的信息。