使用Symfony2更详细地记录错误 [英] Log errors even more verbosely with Symfony2

查看:76
本文介绍了使用Symfony2更详细地记录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在生产日志中使用了以下配置:

I have used the following configuration for my production logging:

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

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

This sends emails like this:

[2012-03-21 21:24:09] security.DEBUG:从 会话[] []

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

[2012-03-21 21:24:09]安全性.调试:从用户中重新加载用户 提供者. [] []

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

[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", 用户名":"Jakob.asdf","_ route":"_ user_settings")[] []

[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]请求.错误: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException: ...未找到包\实体\用户对象. (未捕获的异常)在 /var/www/.../vendor/bundles/Sensio/Bundle/FrameworkExtraBundle/Request/ParamConverter/DoctrineParamConverter.php 第50行[] []

[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:在 会话[] []

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

我真的很想在这里进行堆栈跟踪,或者至少要知道触发错误的控制器中的行号.否则,实际上是在猜测可能出了什么问题.

I would really love to have a stack trace here, or at least the line number in my controller which triggered the error. Otherwise it's really a lot of guessing of what could have gone wrong.

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

Now, the question: Is there any way to achieve such an even more verbose logging?

推荐答案

是可以实现的.

创建一个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"

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

You can tweak it as your requirement.

这篇关于使用Symfony2更详细地记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆