glen/console-logger-serviceprovider

Pimple ConsoleLogger ServiceProvider

3.3.0 2022-02-08 12:48 UTC

This package is auto-updated.

Last update: 2024-09-08 19:10:11 UTC


README

此服务提供程序使您能够在控制台中轻松显示来自服务的日志消息,而无需将 OutputInterface 实例注入到服务中。这需要 Symfony 组件版本 >=2.4。更多关于更改的信息请参阅 Symfony 博客

在您的控制台应用程序中,您现在可以这样做

use Symfony\Component\Console\Application;

$app = require 'app.php';
$console = new Application('My Console Application', '1.0');
// You should only register this service provider when running commands
$app->register(new \glen\ConsoleLoggerServiceProvider());

$console->addCommands(
    array(
    //...
    )
);

$console->run($app['console.input'], $app['console.output']);

您仍将在命令中为命令反馈使用正常的 OutputInterface 实例,但您现在也将从服务记录的任何内容中获得输出。

此处理器将被触发的最小日志级别取决于控制台输出的详细程度设置。默认映射为

  • OutputInterface::VERBOSITY_NORMAL 将显示所有 WARNING 和更高级别的日志
  • OutputInterface::VERBOSITY_VERBOSE (-v) 将显示所有 NOTICE 和更高级别的日志
  • OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) 将显示所有 INFO 和更高级别的日志
  • OutputInterface::VERBOSITY_DEBUG (-vvv) 将显示所有 DEBUG 和更高级别的日志,即所有日志

可以通过 logger.console_logger.handler.verbosity_level_map 构造函数参数自定义此映射

$app->register(new ConsoleLoggerServiceProvider(), [
    'logger.console_logger.handler.verbosity_level_map' => array(
        OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
        OutputInterface::VERBOSITY_NORMAL => Logger::INFO,
        OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
        OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
        OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
    ),
]);