vascek-purchart/console-errors-bundle

用于 Symfony Console 的异常和错误代码的记录


README

用于 Symfony Console 的异常和错误代码的记录。

该组件确保所有未捕获的异常和错误都像控制器中的异常一样被记录。

它还会记录所有以非零退出码结束的命令执行。

以下是一个显示异常、错误和非零退出码的示例:控制台错误报告

以下是相应的日志条目

[2015-05-19 12:41:19] app.ERROR: Exception: Hello Exception! (uncaught exception) at /home/vasek/dev/projects/console-errors-bundle/framework-standard-edition/src/AppBundle/Console/HelloCommand.php line 23 while running console command `hello:world` {"exception":"[object] (Exception(code: 0): Hello Exception! at /home/vasek/dev/projects/console-errors-bundle/framework-standard-edition/src/AppBundle/Console/HelloCommand.php:23)"} []
[2015-05-19 12:41:58] app.ERROR: Symfony\Component\Debug\Exception\ContextErrorException: Notice: Undefined variable: foo (uncaught exception) at /home/vasek/dev/projects/console-errors-bundle/framework-standard-edition/src/AppBundle/Console/HelloCommand.php line 23 while running console command `hello:world` {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\ContextErrorException(code: 0): Notice: Undefined variable: foo at /home/vasek/dev/projects/console-errors-bundle/framework-standard-edition/src/AppBundle/Console/HelloCommand.php:23)"} []
[2015-05-19 12:42:13] app.ERROR: Command `hello:world` exited with status code 123 [] []

配置

配置结构以及列出的默认值

# config/packages/console_errors.yml
console_errors:
    exceptions:
        # Enable logging for exceptions.
        enabled: true
        # Log level with which exceptions should be logged (accepts string or integer values).
        log_level: error
        # Priority with which the listener will be registered.
        listener_priority: 0

    exit_code:
        # Enable logging for non-zero exit codes.
        enabled: true
        # Log level with which exit codes should be logged (accepts string or integer values).
        log_level: error
        # Priority with which the listener will be registered.
        listener_priority: 0

Symfony 默认始终将错误转换为 PHP 异常。警告和通知默认只在开发环境中转换为异常。如果您想配置应用程序始终将警告和通知转换为异常,请使用 debug.error_handler.throw_at 参数(有关其他可用值,请参阅 PHP 手册

# config/packages/framework.yml
parameters:
    debug.error_handler.throw_at: -1

您还可以覆盖内部使用的服务,例如,如果您使用非标准记录器,您可以使用 别名 提供自定义实例

services:
    my_logger:
        class: 'Monolog\Logger'
        arguments:
            - 'my_channel'

    vasek_purchart.console_errors.console.logger: '@my_logger'

安装

使用 Composer 安装包 vasek-purchart/console-errors-bundle

composer require vasek-purchart/console-errors-bundle

在应用程序中注册该组件

// config/bundles.php
return [
	// ...
	VasekPurchart\ConsoleErrorsBundle\ConsoleErrorsBundle::class => ['all' => true],
];