xi/error-bundle

Symfony2的错误处理和断言包

v1.1 2014-07-20 20:28 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:23:37 UTC


README

这是一个用于错误和异常处理及记录的Symfony2包。它还包括用于根据环境格式化用户可读异常信息的组件/服务。

Build Status

安装

此包需要

  • PHP >=5.3
  • Symfony >=2.1
  • Monolog >= 1.3

由于1.3是第一个PSR-3兼容版本,因此需要Monolog 1.3或更高版本以及兼容Symfony的Monolog桥接器。

添加到composer.json中

"xi/error-bundle": "1.*"

有关更多版本信息,请参阅Packagist:https://packagist.org.cn/packages/xi/error-bundle

异常记录

将XiErrorBundle包含在您的AppKernel.php中,即可自动进行异常记录。日志创建在%kernel.logs_dir%/exception.%kernel.environment%.log

<?php
...
    public function registerBundles()
    {
        $bundles = array(
        ...
            new Xi\Bundle\ErrorBundle\XiErrorBundle()
        ...
        );
    }
...

为什么使用单独的异常记录?

默认的Symfony2日志包含大量其他内容,这些内容对调试和日志审核过程帮助不大。此记录器还包括异常的堆栈跟踪。

它处理错误→异常转换吗?

从Symfony 2.2开始,错误会自动转换为异常。

异常信息格式化

在开发过程中,任何信息都有助于在错误发生时立即调试代码。在生产环境中,最好隐藏实际的、可能非常详细的异常信息以供公众查看,并显示一些通用的错误信息。

异常信息格式化组件/服务接受一个异常,并返回原始异常信息或一个通用的错误信息(取决于当前环境)。默认情况下,原始异常信息旨在在"test""dev"环境中显示给开发者,而在其他环境中,最终用户会看到一个更通用的错误信息。

<?php
    $message = 'Everything went better than expected.';

    try {
        # try something dangerous
    } catch (Exception $e) {
        # use the service...
        $service = $this->get('xi_error.exception_formatter');
        $message = $service->formatMessage($e, 'could not process your form, please try again');

        # ...or the component directly
        #$message = \Xi\Bundle\ErrorBundle\Component\ExceptionFormatter::formatMessage(
        #    $e,
        #    'could not process your form, please try again',
        #    $this->get('kernel')->getEnvironment()
        #);
    }

    return new Response($message);
    ...