How to set configuration parameter from console argument?

I am new to symfony.

I am trying to change the formatting of the output Monolog file using the console argument 'format = json'.

In short, I want to run any console command as follows:

app/console my_command --format=json # xml / txt / my own

... and get the result of the LoggerInterface in the requested format.

For example, I set the default formatter in the configuration:

monolog:
    handlers:
        console:
            type:   console
            channels: [!event, !doctrine]
            formatter: json_formatter
services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter

When I create some MyEventListener :: onConsoleCommand ( as described here ), I cannot change the parameter package because it is already compiled: It is not possible to call set () on a frozen ParameterBag. "

Up2: My services configuration in this case is as follows:

services:
    kernel.listener.command_dispatch:
        class: My\Listener\MyEventListener
        autowire: true
        tags:
            - { name: kernel.event_listener, event: console.command }

On the other hand, I can register the console option in the source file:

# app/console
$loader = require __DIR__.'/autoload.php';
# ...
$application->getDefinition()->addOption(
    new InputOption(
        'formatter',
        'f',
        InputOption::VALUE_OPTIONAL,
        'The logs output formatter',
        'json_formatter'
    )
);

. $application- > getKernel() → getContainer() - .

, Symfony2 ?

, , ? YAML?

.

UP3: , :

SYMFONY__LOG__FORMATTER=json_formatter app/console my_command
monolog:
    handlers:
        console:
            type: console
            #...
            formatter: '%log.formatter%'
+4
2

CommandEvents::COMMAND, , . , , . , , . .

, , :

class LogFormatterEventListener
{
    private $container;
    private $consoleHandler;

    public function __construct(ContainerInterface $container, HandlerInterface $consoleHandler)
    {
        $this->container = $container;
        $this->consoleHandler = $consoleHandler;
    }

    public function onConsoleCommand(ConsoleCommandEvent $event)
    {
        $inputDefinition = $event->getCommand()->getApplication()->getDefinition();

        $inputDefinition->addOption(
            new InputOption('logformat', null, InputOption::VALUE_OPTIONAL, 'Format of your logs', null)
        );

        // merge the application input definition
        $event->getCommand()->mergeApplicationDefinition();

        $input = new ArgvInput();

        // we use the input definition of the command
        $input->bind($event->getCommand()->getDefinition());

        $formatter = $input->getOption('logformat');
        if ($formatter /** && $this->container->has($formatter) **/) {
            $this->consoleHandler->setFormatter(
                $this->container->get($formatter);
            );
        }
    }
}
+1

( ):

:

monolog:
    handlers:
        console:
            type:   console
            channels: [!event, !doctrine]
            formatter: "%log.formatter%"
services:
    json_formatter:
        class: Monolog\Formatter\JsonFormatter

:

# colored plain text
app/console my_command

# json
SYMFONY__LOG__FORMATTER=json_formatter app/console my_command
0

Source: https://habr.com/ru/post/1652024/


All Articles