sto*_*fln 20 php logging symfony
我使用以下配置进行生产日志记录:
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
Run Code Online (Sandbox Code Playgroud)
这会发送如下电子邮件:
[2012-03-21 21:24:09] security.DEBUG:从session [] []中读取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"(参数:"_ control":"... Bundle\Controller\UserController :: settingsAction","username": "Jakob.asdf","_ lute":"_ user_settings")[] []
[2012-03-21 21:24:09] request.ERROR:Symfony\Component\HttpKernel\Exception\NotFoundHttpException:...找不到Bundle\Entity\User对象.(未捕获的例外)/var/www/.../vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DoctrineParamConverter.php第50行[] []
[2012-03-21 21:24:09] security.DEBUG:在session [] []中写入SecurityContext
我真的很想在这里有一个堆栈跟踪,或者至少我的控制器中的行号触发了错误.否则,它真的很多猜测可能出错的地方.
现在,问题是:有没有办法实现这种更加冗长的日志记录?
Mun*_*Das 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后注册监听器.
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"
Run Code Online (Sandbox Code Playgroud)
您可以根据需要调整它.
我喜欢Symfony文档中的解决方案.您所要做的就是将以下代码添加到您的services.yml文件中:
services:
my_service:
class: Monolog\Processor\IntrospectionProcessor
tags:
- { name: monolog.processor }
Run Code Online (Sandbox Code Playgroud)
这使用IntrospectionProcessor经过测试的处理器向您的日志添加更多信息.它很可能会提取您关心的信息.
| 归档时间: |
|
| 查看次数: |
7941 次 |
| 最近记录: |